我的数据如下:
我想将数据汇总成多级行和列,我想让它看起来像这样
我成功创建了多级行,但无法将price_flag放在列中。我使用下面提到的代码来做那个
aggregate(data$year, by = list(data$class, data$year), FUN = length)
非常感谢帮助。
答案 0 :(得分:0)
dat
ID price_flag Year Class
1 123 5k-10k 2016 A
2 234 10k-25k 2017 B
3 345 25k-50k 2018 C
4 456 5k-10k 2017 A
5 567 10k-25k 2018 C
ftable(rev(dat[-1]))
price_flag 10k-25k 25k-50k 5k-10k
Class Year
A 2016 0 0 1
2017 0 0 1
2018 0 0 0
B 2016 0 0 0
2017 1 0 0
2018 0 0 0
C 2016 0 0 0
2017 0 0 0
2018 1 1 0
甚至:
ftable(dat[4:2])
price_flag 10k-25k 25k-50k 5k-10k
Class Year
A 2016 0 0 1
2017 0 0 1
2018 0 0 0
B 2016 0 0 0
2017 1 0 0
2018 0 0 0
C 2016 0 0 0
2017 0 0 0
2018 1 1 0