【问题标题】:Format data frame rows rather than columns格式化数据框行而不是列
【发布时间】:2020-03-24 15:49:36
【问题描述】:

我有一个趋势数据表。

ga_sessions_combined <-
structure(list(Metric = structure(1:7, .Label = c("Users", "Engaged Users", 
"Transactions", "Revenue", "ConversionRate", "Bounce Rate", "$/User"
), class = "factor"), ym_201904 = c(157664, 79295, 5764, 609172.887628, 
0.0365587578648265, 0.497063375279075, 3.86374116873858), ym_201905 = c(199340, 
103879, 5744, 673063.435872, 0.0288150897963279, 0.478885321561152, 
3.3764594956958), ym_201906 = c(169971, 90557, 4899, 566247.290325, 
0.0288225638491272, 0.467220878855805, 3.33143471724588), ym_201907 = c(161346, 
88059, 4223, 580408.759911, 0.0261735648854016, 0.454222602357666, 
3.5972925260682), ym_201908 = c(132702, 70701, 3106, 424807.71545, 
0.0234058265888984, 0.467219785685219, 3.20121562184443), ym_201909 = c(164160, 
96124, 3841, 724958.93068, 0.0233979044834308, 0.414449317738791, 
4.41617282334308), ym_201910 = c(217227, 118041, 4448, 798116.2282, 
0.0204762759693777, 0.456600698808159, 3.67411154322437), ym_201911 = c(970864, 
604606, 27713, 4859788.602792, 0.0285446777303515, 0.37724954267539, 
5.00563271765355), ym_201912 = c(1180689, 671162, 59536, 9447240.17602, 
0.0504247943361884, 0.431550560731912, 8.00146370129645), ym_202001 = c(216816, 
109637, 5057, 738079.024166, 0.0233239244336211, 0.494331599143975, 
3.40417231277212), ym_202002 = c(204113, 145975, 4847, 720506.474953, 
0.0237466501398735, 0.284832421256853, 3.52993917561841), ym_202003 = c(324266, 
229438, 8341, 1196234.593648, 0.0257227091338592, 0.292438923599761, 
3.68905341185323)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-7L), .Names = c("Metric", "ym_201904", "ym_201905", "ym_201906", 
"ym_201907", "ym_201908", "ym_201909", "ym_201910", "ym_201911", 
"ym_201912", "ym_202001", "ym_202002", "ym_202003"))

当我在闪亮的应用程序中运行这个数据框时,它看起来像这样:

我想根据“度量”列来格式化表格,几乎像 dplyr 动词,但列名是第一行。

对于前 3 行,Users、Engaged Users 和 Transactions,我想使用 scales::comma_format() 格式化,以便使用逗号表示千位,例如 1,000。

对于收入和“$/User”行,我想用scales::dollar_format() 格式化

对于转化率和跳出率行,我想将它们格式化为scales::percent_format()

我该怎么做?

【问题讨论】:

  • 您应该使用DT 函数,例如formatCurrency。更多详情请见here
  • 感谢您的提示,但这些提示似乎仍然适用于列而不是行
  • 我的退路可能是转置,将现有列设为行名,将 Metric 列设为列。然后转回来。
  • 是的,我认为这是最简单的方法
  • 正如我在@Alan Dursun 的回答中所说,转置数据,在其上使用DT 函数并将其转回将不起作用,因为使用DT 函数意味着更改@987654332 中的数据@ 并且我们无法转置 datatable 对象(因此我们无法将您的数据转回其原始形式)

标签: r


【解决方案1】:

也许不是您正在寻找的答案,但转置您的数据框更容易。这是tidyr 方法:

library(tidyr)
ga_sessions_combined %>% 
  gather(key = period, value = value, 2:ncol(ga_sessions_combined)) %>% 
  spread(key = names(ga_sessions_combined)[1], value = "value")

编辑:

如果你想保持宽格式,我认为这可行,但一切都转换为字符:

ga_sessions_combined %>% 
  gather(key = period, value = value, 2:ncol(ga_sessions_combined)) %>% 
  spread(key = names(ga_sessions_combined)[1], value = "value") %>% 
  mutate_at(vars(matches("Users|Engaged Users|Transactions")), funs(prettyNum(., big.mark=","))) %>% 
  mutate_at(vars(matches("Rate")), funs(scales::percent(., accuracy = 0.01))) %>% 
  mutate_at(vars(contains("$/User"), contains("Revenue")), funs(scales::dollar(.))) %>% t()

如果可以接受长格式,则只需将t() 放在末尾即可。

【讨论】:

  • 问题是我们需要更改 datatable 对象中的这些数据以在其上应用 format* 函数,但是我们不能使用 spreadgather datatable对象回到原来的显示
猜你喜欢
  • 2016-02-28
  • 2018-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-17
  • 1970-01-01
相关资源
最近更新 更多