计算数据框的多列频率

时间:2016-02-01 17:52:49

标签: r

如果我有一个包含多个列的数据框(我不知道确切的列数),但我知道stock_和另一个是类似的数字

stock_1        stock_2  stock_3       stock_4
Google         NA       Yahoo         Google
Bing  Search   Yahoo    Bing Search   NA
Google         Yahoo    Yahoo         Bing Search

如何计算所有列的所有单词的频率并得到如下结果:

name          frequency
Google           3
Bing Search      3
Yahoo            4

1 个答案:

答案 0 :(得分:0)

我们可以unlist数据集并使用table

as.data.frame(table(unlist(df1)))
#         Var1 Freq
#1 Bing Search    3
#2      Google    3
#3       Yahoo    4

数据

df1 <- structure(list(stock_1 = c("Google", "Bing Search", "Google"), 
stock_2 = c(NA, "Yahoo", "Yahoo"), stock_3 = c("Yahoo", 
"Bing Search", 
"Yahoo"), stock_4 = c("Google", NA, "Bing Search")), 
.Names = c("stock_1", 
"stock_2", "stock_3", "stock_4"), class = "data.frame", 
 row.names = c(NA, -3L))