在多列中使用 count() 并对结果排序
Use count() in more than one column and order results
我有一个名为 table
的数据框,就像这样
a m g c1 c2 c3 c4
1 2015 5 13 bread wine <NA> <NA>
2 2015 8 30 wine eggs rice cake
3 2015 1 21 wine rice eggs <NA>
...
我想统计c1到c4列的元素并排序
我尝试使用:
library(plyr)
c<-count(table,"c1")
但是我不知道如何计算超过一列。
然后我想使用 arrange(c,desc(freq))
对它们进行排序,但是当我尝试使用一列时,值 NA 始终位于顶部,我只想要前 3 个元素。像这样
c freq
1 wine 3
2 eggs 2
3 rice 2
有人可以帮我解决这个问题吗?谢谢
使用melt
和table
:
df1 <- read.table(text="a m g c1 c2 c3 c4
2015 5 13 bread wine NA NA
2015 8 30 wine eggs rice cake
2015 1 21 wine rice eggs NA", header=TRUE, stringsAsFactors=FALSE)
c_col <- melt(as.matrix(df1[,4:7]))
sort(table(c_col$value),decreasing=TRUE)
wine eggs rice bread cake
3 2 2 1 1
使用 qdaptools
,提供示例数据框(名称为 table
):
library(qdapTools)
counts <- data.frame(count=sort(colSums(mtabulate(table[,4:7])), decreasing=TRUE))
subset(counts,rownames(counts)!='<NA>')[1:3,1,drop=FALSE] #remove <NA>, select top 3 elements
# count
# wine 3
# eggs 2
# rice 2
我有一个名为 table
的数据框,就像这样
a m g c1 c2 c3 c4
1 2015 5 13 bread wine <NA> <NA>
2 2015 8 30 wine eggs rice cake
3 2015 1 21 wine rice eggs <NA>
...
我想统计c1到c4列的元素并排序 我尝试使用:
library(plyr)
c<-count(table,"c1")
但是我不知道如何计算超过一列。
然后我想使用 arrange(c,desc(freq))
对它们进行排序,但是当我尝试使用一列时,值 NA 始终位于顶部,我只想要前 3 个元素。像这样
c freq
1 wine 3
2 eggs 2
3 rice 2
有人可以帮我解决这个问题吗?谢谢
使用melt
和table
:
df1 <- read.table(text="a m g c1 c2 c3 c4
2015 5 13 bread wine NA NA
2015 8 30 wine eggs rice cake
2015 1 21 wine rice eggs NA", header=TRUE, stringsAsFactors=FALSE)
c_col <- melt(as.matrix(df1[,4:7]))
sort(table(c_col$value),decreasing=TRUE)
wine eggs rice bread cake
3 2 2 1 1
使用 qdaptools
,提供示例数据框(名称为 table
):
library(qdapTools)
counts <- data.frame(count=sort(colSums(mtabulate(table[,4:7])), decreasing=TRUE))
subset(counts,rownames(counts)!='<NA>')[1:3,1,drop=FALSE] #remove <NA>, select top 3 elements
# count
# wine 3
# eggs 2
# rice 2