如何使用R将频率转换为文本?

时间:2016-07-16 12:32:43

标签: r text frequency lda

我有这样的数据帧(ID,频率A B C D E)

ID A B C D E    
1  5 3 2 1 0  
2  3 2 2 1 0  
3  4 2 1 1 1

我想将这个数据帧转换为这样的基于测试的文档(ID和它们的频率ABCDE作为单个列中的单词)。然后我可以使用LDA算法来识别每个ID的热门话题。

ID                     Text
1   "A" "A" "A" "A" "A" "B" "B" "B" "C" "C" "D"
2   "A" "A" "A" "B" "B" "C" "C" "D"
3   "A" "A" "A" "A" "B" "B" "C" "D" "E"

2 个答案:

答案 0 :(得分:1)

您可以像这样使用applyrep

apply(df[-1], 1, function(i) rep(names(df)[-1], i))

对于每一行,applyrep函数提供重复每个变量名称的次数。这将返回一个向量列表:

[[1]]
 [1] "A" "A" "A" "A" "A" "B" "B" "B" "C" "C" "D"

[[2]]
[1] "A" "A" "A" "B" "B" "C" "C" "D"

[[3]]
[1] "A" "A" "A" "A" "B" "B" "C" "D" "E"

每个列表元素都是data.frame的一行。

数据

df <- read.table(header=T, text="ID A B C D E    
1  5 3 2 1 0  
2  3 2 2 1 0  
3  4 2 1 1 1")

答案 1 :(得分:1)

我们可以使用data.table

library(data.table)
DT <- setDT(df1)[,.(list(rep(names(df1)[-1], unlist(.SD)))) ,ID]
DT$V1
#[[1]]
#[1] "A" "A" "A" "A" "A" "B" "B" "B" "C" "C" "D"

#[[2]]
#[1] "A" "A" "A" "B" "B" "C" "C" "D"

#[[3]]
#[1] "A" "A" "A" "A" "B" "B" "C" "D" "E"

base R选项为split

lst <- lapply(split(df1[-1], df1$ID), rep, x=names(df1)[-1])
lst
#$`1`
#[1] "A" "A" "A" "A" "A" "B" "B" "B" "C" "C" "D"

#$`2`
#[1] "A" "A" "A" "B" "B" "C" "C" "D"

#$`3`
#[1] "A" "A" "A" "A" "B" "B" "C" "D" "E"

如果我们想要写下&#39; lst&#39;对于csv文件,一个选项是将list转换为data.frame,方法是在末尾添加NA以使长度相等,同时转换为data.framedata.framelist,长度相等(列))

res <- do.call(rbind, lapply(lst, `length<-`, max(lengths(lst))))

或使用stringi

中的便捷功能
library(stringi)
res <- stri_list2matrix(lst, byrow=TRUE)

然后使用write.csv

write.csv(res, "yourdata.csv", quote=FALSE, row.names = FALSE)