在dplyr summary()之后使用dcast()函数
我有一个来自四个不同射箭项目的4000分数据集。数据集中有两种不同的设备类别:复合设备和下弯设备。我需要显示一些按“事件”分组的汇总统计数据,但按“类”分布在一个表中 以下是一些示例数据:在dplyr summary()之后使用dcast()函数,r,dplyr,tidyr,R,Dplyr,Tidyr,我有一个来自四个不同射箭项目的4000分数据集。数据集中有两种不同的设备类别:复合设备和下弯设备。我需要显示一些按“事件”分组的汇总统计数据,但按“类”分布在一个表中 以下是一些示例数据: > results # A tibble: 4,478 x 8 Year Event Class Division Gender Organization Setting Score <dbl> <chr>
> results
# A tibble: 4,478 x 8
Year Event Class Division Gender Organization Setting Score
<dbl> <chr> <chr> <chr> <chr> <chr> <chr> <dbl>
1 2016 NFAA Indoor Nationals Compound Amateur F NFAA Indoor 711
2 2016 NFAA Indoor Nationals Compound Amateur F NFAA Indoor 708
3 2016 NFAA Indoor Nationals Compound Amateur F NFAA Indoor 708
4 2016 NFAA Indoor Nationals Compound Amateur F NFAA Indoor 702
5 2016 NFAA Indoor Nationals Compound Amateur F NFAA Indoor 700
6 2016 NFAA Indoor Nationals Compound Amateur F NFAA Indoor 700
7 2016 NFAA Indoor Nationals Compound Amateur F NFAA Indoor 699
8 2016 NFAA Indoor Nationals Compound Amateur F NFAA Indoor 696
9 2016 NFAA Indoor Nationals Compound Amateur F NFAA Indoor 694
10 2016 NFAA Indoor Nationals Compound Amateur F NFAA Indoor 690
# … with 4,468 more rows
到目前为止,传播这些数据的最后一步我还没有完成。有什么建议吗?谢谢。以下是一种可能的解决方案。从您的
percentile\u摘要
tible开始,您可以使用包data.table
来使用函数dcast
,该函数在value.var
参数中有多列,即
library(data.table)
df <- dcast(setDT(percentile_summaries), Event ~ Class, value.var = c("p10", "p50", "p90"))
在这里,列的顺序不是期望的,因为“复合”和“下弯”是交替的。要订购它们,请使用
df[,c(1,2,4,6,3,5,7)]
输出:
Event p10_Compound p50_Compound p90_Compound p10_Recurve p50_Recurve p90_Recurve
NFAA_Field_Nationals 504 538 555 398 463 496
NFAA_Indoor_Nationals 656 704 718 464 554 626
USA_Archery_Indoor_Nationals 1026 1116 1166 706 959 1105
USA_Archery_Outdoor_Nationals 1148 1328 1398 860 1096 1252
然后,您可以继续使用所需的HTML表。一种可能的解决方案如下。从您的
percentile\u摘要
tible开始,您可以使用包data.table
来使用函数dcast
,该函数在value.var
参数中有多列,即
library(data.table)
df <- dcast(setDT(percentile_summaries), Event ~ Class, value.var = c("p10", "p50", "p90"))
在这里,列的顺序不是期望的,因为“复合”和“下弯”是交替的。要订购它们,请使用
df[,c(1,2,4,6,3,5,7)]
输出:
Event p10_Compound p50_Compound p90_Compound p10_Recurve p50_Recurve p90_Recurve
NFAA_Field_Nationals 504 538 555 398 463 496
NFAA_Indoor_Nationals 656 704 718 464 554 626
USA_Archery_Indoor_Nationals 1026 1116 1166 706 959 1105
USA_Archery_Outdoor_Nationals 1148 1328 1398 860 1096 1252
然后,您可以继续使用所需的HTML表格。请使用
dput(head(results,20))
提供示例数据。感谢您的提示。我以后会这样做。请使用dput(head(results,20))
提供示例数据。感谢您的提示。“我以后会这么做的。”蒂姆·威尔逊很高兴这有帮助!:)@蒂姆·威尔逊很高兴这有帮助!:)
Event p10_Compound p50_Compound p90_Compound p10_Recurve p50_Recurve p90_Recurve
NFAA_Field_Nationals 504 538 555 398 463 496
NFAA_Indoor_Nationals 656 704 718 464 554 626
USA_Archery_Indoor_Nationals 1026 1116 1166 706 959 1105
USA_Archery_Outdoor_Nationals 1148 1328 1398 860 1096 1252