【发布时间】: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