将频率表合并为一个数据帧 [英] Combine frequency tables into a single data frame

查看:95
本文介绍了将频率表合并为一个数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a list in which each list item is a word frequency table derived from using "table()" on a different sample text. Each table is, therefore, a different length. I want to now convert the list into a single data frame in which each column is a word each row is a sample text. Here is a dummy example of my data:

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)

因此,人们得到了这种结构:

so, one gets this sort of structure:

> 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-一个数据帧的长度/),例如

I now need to convert this list (myList) into a single data frame. I thought I could do this with plyr, along the lines of what is done here (http://ryouready.wordpress.com/2009/01/23/r-combining-vectors-or-data-frames-of-unequal-length-into-one-data-frame/), e.g.

library(plyr)
l <- myList
do.call(rbind.fill, l)

但是似乎我的表"对象不能很好地播放.我曾尝试将它们转换为dfs以及矢量,但都无法正常工作.

But it seems that my "table" objects do not play nice. I tried converting them to dfs and also to vectors, but none of that worked quite right.

推荐答案

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)

这篇关于将频率表合并为一个数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆