2

如何创建垂直排列的值频率组合表,

> df = data.frame(fruit=c("apple", "banana", "cherry", "cherry", "apple", "banana", "apple", "date"));
> table(df$fruit)

 apple banana cherry   date 
     3      2      2      1

到目前为止,一切都很好。但我想要这样的东西(例如,基于频率的基本操作和值的子集):

Fruit   Freq
"apple"   3
"banana"  2
"cherry"  2
"date"    1

在 SQL 中,这将是SELECT fruit, COUNT(*) AS Freq FROM df GROUP BY fruit,并且会产生一个类似于此问题起点的表: https ://stats.stackexchange.com/questions/15574/how-to-convert-a-frequency-table-into-值向量

在R中是否有一种简单的方法可以做到这一点?(或者,这是否表明思维方式过于“SQL”而“R”不够?)

4

5 回答 5

6
data.frame(table(df))
#       df Freq
# 1  apple    3
# 2 banana    2
# 3 cherry    2
# 4   date    1

也许

setNames(data.frame(table(df)), c("Fruit", "Freq"))
#    Fruit Freq
# 1  apple    3
# 2 banana    2
# 3 cherry    2
# 4   date    1
于 2012-12-05T17:27:05.013 回答
4

仅使用base R,您可以将其转换为一个矩阵,该矩阵将是面向列的,其值作为row.names。

as.matrix(table(df))
于 2012-12-05T17:27:32.220 回答
3

使用meltreshape2 包中的函数

DF <- table(df$fruit)

library(reshape2)
melt(DF)
colnames(result) <- c('Fruit', 'Freq')
result


  Fruit Freq
1  apple    3
2 banana    2
3 cherry    2
4   date    1
于 2012-12-05T17:26:29.653 回答
0

只是添加一个不同的答案:

df = data.frame(fruit=c("apple", "banana", "cherry", "cherry", "apple", "banana", "apple", "date"));
df
   fruit
1  apple
2 banana
3 cherry
4 cherry
5  apple
6 banana
7  apple
8   date
freq <- unlist(lapply(unique(df$fruit),function(x) length(which(df$fruit ==x))))

freq
[1] 3 2 2 1
df.new <- data.frame(fruits = unique(df$fruit),freq)
df.new
  fruits freq
1  apple    3
2 banana    2
3 cherry    2
4   date    1
于 2012-12-06T09:28:51.443 回答
0

如果你想坚持 SQL 思维方式,总有“ sqldf”包!

df = data.frame(fruit=c("apple", "banana", "cherry", "cherry", 
                        "apple", "banana", "apple", "date"))
library(sqldf)
sqldf("SELECT fruit, COUNT(*) AS Freq FROM df GROUP BY fruit")
#    fruit Freq
# 1  apple    3
# 2 banana    2
# 3 cherry    2
# 4   date    1

那里!你已经知道答案了;)

于 2012-12-06T04:38:16.283 回答