如何使用R语言在一列中统计字符串?

时间:2016-07-06 14:40:04

标签: r string

我有一张这样的表

enter image description here

现在我想要在“关键字”列中统计k1,k2,k3 ...的频率。我怎么能这样做。

我想使用像“string”

这样的包

文件链接wl.csv(中文)

该文件有500行。每行都是一张纸。每篇论文都有1~3个关键词。我想统计关键字的频率。

2 个答案:

答案 0 :(得分:0)

您不需要任何包。此外,没有string这样的包。你在想stringr吗?

您可以使用strsplittableunlist来获取所有观察结果的频率计数。

df <- data.frame(title=c("A","B","C"),keywords=c("k1;k2;k3","k4;k1","k1;k2"),stringsAsFactors=FALSE)

list_of_keywords <- strsplit(df$keywords,";")
table(unlist(list_of_keywords))

结果将是:

k1 k2 k3 k4 
 3  2  1  1 

答案 1 :(得分:-1)

如果df是您的数据框:

df <- data.frame(Title = paste0("title",toupper(letters[1:7])), 
                 keywords = c("k1;k2;k3", "k4;k1", "k1;k2", "k3;k4", "k2", "k5", "k4;k2;k5"))


library(dplyr)
library(stringr)
temp <- df$keywords %>% str_split(";")

# Obtain the frequency by flattening the list and using the table function
table(unlist(temp))