从列表中生成频率数据帧,同时在R中保留行
我有一个如下列表:从列表中生成频率数据帧,同时在R中保留行,r,plyr,reshape2,R,Plyr,Reshape2,我有一个如下列表: >AP $CMP1 [1] 411050384 411050456 411050456 411058568 $CMP2 [1] 411050384 411050456 $CMP3 [1] 411050384 411050456 411058568 428909002 428909002 我想把列表转换成一个数据框,使用每个唯一的条目作为列名,数据框中的条目是列表“CMP”中每个成员的频率计数。这就是我想要的数据帧的样子 411050384 4110504
>AP
$CMP1
[1] 411050384 411050456 411050456 411058568
$CMP2
[1] 411050384 411050456
$CMP3
[1] 411050384 411050456 411058568 428909002 428909002
我想把列表转换成一个数据框,使用每个唯一的条目作为列名,数据框中的条目是列表“CMP”中每个成员的频率计数。这就是我想要的数据帧的样子
411050384 411050456 411058568 428909002
CMP1 1 2 1 0
CMP2 1 1 0 0
CMP3 1 1 1 2
我已经查看了“plyr”和“Reforme2”软件包的文档,但我没有任何运气。感谢您的帮助。谢谢。我不认为这是最优雅的,但它很管用 您的数据:
CMP1=c(411050384, 411050456, 411050456, 411058568)
CMP2=c(411050384, 411050456)
CMP3=c(411050384, 411050456, 411058568, 428909002, 428909002)
AP=list(CMP1, CMP2, CMP3)
names(AP)=c('CMP1', 'CMP2', 'CMP3')
首先对列表中的每个元素使用表
,以获取频率。然后我使用Map
将列表中每个元素的名称添加为变量,并使用rbind
将其放在一起
x<-lapply(lapply(AP, table), cbind)
x<-Map(cbind, x, id = names(AP))
x1<-do.call('rbind',x)
我并不认为这是最优雅的,但它确实有效 您的数据:
CMP1=c(411050384, 411050456, 411050456, 411058568)
CMP2=c(411050384, 411050456)
CMP3=c(411050384, 411050456, 411058568, 428909002, 428909002)
AP=list(CMP1, CMP2, CMP3)
names(AP)=c('CMP1', 'CMP2', 'CMP3')
首先对列表中的每个元素使用表
,以获取频率。然后我使用Map
将列表中每个元素的名称添加为变量,并使用rbind
将其放在一起
x<-lapply(lapply(AP, table), cbind)
x<-Map(cbind, x, id = names(AP))
x1<-do.call('rbind',x)
我并不认为这是最优雅的,但它确实有效 您的数据:
CMP1=c(411050384, 411050456, 411050456, 411058568)
CMP2=c(411050384, 411050456)
CMP3=c(411050384, 411050456, 411058568, 428909002, 428909002)
AP=list(CMP1, CMP2, CMP3)
names(AP)=c('CMP1', 'CMP2', 'CMP3')
首先对列表中的每个元素使用表
,以获取频率。然后我使用Map
将列表中每个元素的名称添加为变量,并使用rbind
将其放在一起
x<-lapply(lapply(AP, table), cbind)
x<-Map(cbind, x, id = names(AP))
x1<-do.call('rbind',x)
我并不认为这是最优雅的,但它确实有效 您的数据:
CMP1=c(411050384, 411050456, 411050456, 411058568)
CMP2=c(411050384, 411050456)
CMP3=c(411050384, 411050456, 411058568, 428909002, 428909002)
AP=list(CMP1, CMP2, CMP3)
names(AP)=c('CMP1', 'CMP2', 'CMP3')
首先对列表中的每个元素使用表
,以获取频率。然后我使用Map
将列表中每个元素的名称添加为变量,并使用rbind
将其放在一起
x<-lapply(lapply(AP, table), cbind)
x<-Map(cbind, x, id = names(AP))
x1<-do.call('rbind',x)
这个怎么样
res <- t(sapply(AP, function(y) sapply(unique(unlist(AP)), function(x) sum(x == y))))
colnames(res) <- unique(unlist(AP))
res
411050384 411050456 411058568 428909002
CMP1 1 2 1 0
CMP2 1 1 0 0
CMP3 1 1 1 2
res这个怎么样
res <- t(sapply(AP, function(y) sapply(unique(unlist(AP)), function(x) sum(x == y))))
colnames(res) <- unique(unlist(AP))
res
411050384 411050456 411058568 428909002
CMP1 1 2 1 0
CMP2 1 1 0 0
CMP3 1 1 1 2
res这个怎么样
res <- t(sapply(AP, function(y) sapply(unique(unlist(AP)), function(x) sum(x == y))))
colnames(res) <- unique(unlist(AP))
res
411050384 411050456 411058568 428909002
CMP1 1 2 1 0
CMP2 1 1 0 0
CMP3 1 1 1 2
res这个怎么样
res <- t(sapply(AP, function(y) sapply(unique(unlist(AP)), function(x) sum(x == y))))
colnames(res) <- unique(unlist(AP))
res
411050384 411050456 411058568 428909002
CMP1 1 2 1 0
CMP2 1 1 0 0
CMP3 1 1 1 2
res您可以从qdapTools
library(qdapTools)
mtabulate(AP)
# 411050384 411050456 411058568 428909002
#CMP1 1 2 1 0
#CMP2 1 1 0 0
#CMP3 1 1 1 2
或melt/acast
fromrestrape2
library(reshape2)
acast(melt(AP), L1~value, length)
# 411050384 411050456 411058568 428909002
#CMP1 1 2 1 0
#CMP2 1 1 0 0
#CMP3 1 1 1 2
或使用base R
table(stack(AP)[2:1])
# values
#ind 411050384 411050456 411058568 428909002
# CMP1 1 2 1 0
# CMP2 1 1 0 0
# CMP3 1 1 1 2
您可以从qdapTools
library(qdapTools)
mtabulate(AP)
# 411050384 411050456 411058568 428909002
#CMP1 1 2 1 0
#CMP2 1 1 0 0
#CMP3 1 1 1 2
或melt/acast
fromrestrape2
library(reshape2)
acast(melt(AP), L1~value, length)
# 411050384 411050456 411058568 428909002
#CMP1 1 2 1 0
#CMP2 1 1 0 0
#CMP3 1 1 1 2
或使用base R
table(stack(AP)[2:1])
# values
#ind 411050384 411050456 411058568 428909002
# CMP1 1 2 1 0
# CMP2 1 1 0 0
# CMP3 1 1 1 2
您可以从qdapTools
library(qdapTools)
mtabulate(AP)
# 411050384 411050456 411058568 428909002
#CMP1 1 2 1 0
#CMP2 1 1 0 0
#CMP3 1 1 1 2
或melt/acast
fromrestrape2
library(reshape2)
acast(melt(AP), L1~value, length)
# 411050384 411050456 411058568 428909002
#CMP1 1 2 1 0
#CMP2 1 1 0 0
#CMP3 1 1 1 2
或使用base R
table(stack(AP)[2:1])
# values
#ind 411050384 411050456 411058568 428909002
# CMP1 1 2 1 0
# CMP2 1 1 0 0
# CMP3 1 1 1 2
您可以从qdapTools
library(qdapTools)
mtabulate(AP)
# 411050384 411050456 411058568 428909002
#CMP1 1 2 1 0
#CMP2 1 1 0 0
#CMP3 1 1 1 2
或melt/acast
fromrestrape2
library(reshape2)
acast(melt(AP), L1~value, length)
# 411050384 411050456 411058568 428909002
#CMP1 1 2 1 0
#CMP2 1 1 0 0
#CMP3 1 1 1 2
或使用base R
table(stack(AP)[2:1])
# values
#ind 411050384 411050456 411058568 428909002
# CMP1 1 2 1 0
# CMP2 1 1 0 0
# CMP3 1 1 1 2
在simllar spirit中,但可能更容易do.call(rbind,lappy(AP,function(ii)table(factor(ii,levels=unique(unlist(AP‘‘‘‘‘‘‘‘‘‘‘)
在simllar spirit中,但在do.call中可能更容易(AP,功能(ii)表(因子(ii,级别=唯一(未列出(AP)俬俬俬俬俬)
,但可能更容易do.call(rbind,lappy)(AP,功能(ii)表(因子(ii,级别=唯一(未列出(AP俬俬俬俬))))