data.frame(Name = rep(c("A", "B", "C"), each = 3),
Score = rep(1:3, each = 3),
Frequency = do.call(c, lapply(df[], table)))
# Name Score Frequency
# A.1 A 1 3
# A.2 A 1 3
# A.3 A 1 2
# B.1 B 2 1
# B.2 B 2 4
# B.3 B 2 3
# C.1 C 3 5
# C.2 C 3 2
# C.3 C 3 1
df%>%
gather(Name,Score,A:C)%>%
group_by(Name,Score)%>%
summarise(Frequency=n())%>%
ungroup
Name Score Frequency
<chr> <dbl> <int>
1 A 1 3
2 A 2 3
3 A 3 2
4 B 1 1
5 B 2 4
6 B 3 3
7 C 1 5
8 C 2 2
9 C 3 1
4条答案
按热度按时间xfyts7mz1#
dplyr
解决方案可能更具可扩展性,但另一种基于R的方法是:使用do.call
沿着lapply
和table
,然后将其全部放入 Dataframe 中:l5tcr1uw2#
7rfyedvj3#
使用
base R
deyfvvtc4#
我们可以将数据转换为长格式,然后计算频率