gpt4 book ai didi

r - 将频率表合并为一个数据帧

转载 作者:行者123 更新时间:2023-12-04 11:49:34 25 4
gpt4 key购买 nike

我有一个列表,其中每个列表项都是一个单词频率表,该单词频率表是通过在不同的示例文本上使用“table()”派生而来的。因此,每个表的长度都不同。我现在想将列表转换成单个数据框,其中每一列都是一个单词,每一行都是示例文本。这是我的数据的虚拟示例:

t1<-table(strsplit(tolower("this is a test in the event of a real word file you would see many more words here"), "\\W"))

t2<-table(strsplit(tolower("Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal"), "\\W"))

t3<-table(strsplit(tolower("Ask not what your country can do for you - ask what you can do for your country"), "\\W"))

myList <- list(t1, t2, t3)

因此,人们得到了这种结构:
> class(myList[[3]])
[1] "table"

> myList[[3]]

ask can country do for not what you your
2 2 2 2 2 2 1 2 2 2

现在,我需要将此列表(myList)转换为单个数据帧。我以为可以用plyr做到这一点,就像这里所做的一样(http://ryouready.wordpress.com/2009/01/23/r-combining-vectors-or-data-frames-of-uniqual-一个数据帧的长度/),例如
library(plyr)
l <- myList
do.call(rbind.fill, l)

但是看来我的“ table ”对象玩不好。我尝试将它们转换为dfs以及向量,但没有一个能正常工作。

最佳答案

freqs.list <- mapply(data.frame,Words=seq_along(myList),myList,SIMPLIFY=FALSE,MoreArgs=list(stringsAsFactors=FALSE))
freqs.df <- do.call(rbind,freqs.list)
res <- reshape(freqs.df,timevar="Words",idvar="Var1",direction="wide")
head(res)

关于r - 将频率表合并为一个数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9250344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com