如何将向量转换成不同的列,使NAs留在剩余的单元格中 [英] How to rbind vectors into different columns, leaving NAs in remaining cells
问题描述
下面我提供一个随机抽样输入( lv
)和一个差的解决方案来生成所需的输出,通过重复 NA
手动构建生成的数据框架通过名称组合每个输入向量。
set.seed(1);
lv< - list(a = sample(30,5),b = sample(30,3),c = sample(30,7),d = sample(30,2));
lv;
## $ a
## [1] 8 11 17 25 6
##
##
$ b ## [1] 27 28 19
##
## $ c
## [1] 19 2 6 5 18 10 30
##
## $ d
## [1 ] 15 21
##
with(lv,data.frame(a = c(a,rep(NA,length(b)+ length(c)+ length(d))),b = c(rep(NA,长度(a)),b,rep(NA,长度(c)+长度(d))),c = c(rep(NA,长度(a)+长度(b)),c ,代表(NA,长度(d))),d = c(rep(NA,长度(a)+长度(b)+长度(c)),d)))
## abcd
## 1 8 NA NA NA
## 2 11 NA NA NA
## 3 17 NA NA NA
## 4 25 NA NA NA
## 5 6 NA NA NA
## 6 NA 27 NA NA
## 7 NA 28 NA NA
## 8 NA 19 NA NA
# #9 NA NA 19 NA
## 10 NA NA 2 NA
## 11 NA NA 6 NA
## 12 NA NA 5 NA
## 13 NA NA 18 NA
## 14 NA NA 10 NA
## 15 NA NA 30 NA
## 16 NA NA NA 15
## 17 NA NA NA 21
注意:您不必使用 rbind()
,I只是觉得这是介绍问题的最清楚的方法。另一种思考方式是我想将 cbind()
向量转换成不同的(从不重叠)的行。
尝试
library(reshape2)
库(data.table)
dcast(setDT(melt(lv))[,rn:=。I],rn〜L1,value.var ='value')
或
dcast(setDT(melt(lv))keep.rownames = TRUE),
as.numeric(rn)〜L1,value.var ='value')
或者由@David Arenburg建议
recast(lv,seq_along(unlist(lv))〜L1)
或使用 base R
d1< - stack(lv)
reshape(transform(d1,rn = 1:nrow(d1)),idvar ='rn ',
timevar ='ind',direction ='wide')
Let's say I have an unspecified number of vectors of different lengths, and I want to effectively rbind them together, with the caveat that they must each occupy a different column in the resulting data.frame. You may assume that the vectors are contained in a list, but you cannot depend on any component names that may be defined within the list.
Below I present a random sample input (lv
) and a poor solution to generate the required output that manually builds the resulting data.frame by repeating NA
and combining each input vector by name.
set.seed(1);
lv <- list(a=sample(30,5),b=sample(30,3),c=sample(30,7),d=sample(30,2));
lv;
## $a
## [1] 8 11 17 25 6
##
## $b
## [1] 27 28 19
##
## $c
## [1] 19 2 6 5 18 10 30
##
## $d
## [1] 15 21
##
with(lv,data.frame(a=c(a,rep(NA,length(b)+length(c)+length(d))),b=c(rep(NA,length(a)),b,rep(NA,length(c)+length(d))),c=c(rep(NA,length(a)+length(b)),c,rep(NA,length(d))),d=c(rep(NA,length(a)+length(b)+length(c)),d)));
## a b c d
## 1 8 NA NA NA
## 2 11 NA NA NA
## 3 17 NA NA NA
## 4 25 NA NA NA
## 5 6 NA NA NA
## 6 NA 27 NA NA
## 7 NA 28 NA NA
## 8 NA 19 NA NA
## 9 NA NA 19 NA
## 10 NA NA 2 NA
## 11 NA NA 6 NA
## 12 NA NA 5 NA
## 13 NA NA 18 NA
## 14 NA NA 10 NA
## 15 NA NA 30 NA
## 16 NA NA NA 15
## 17 NA NA NA 21
Note: You don't have to use rbind()
, I just felt that that was the clearest way to introduce the problem. Another way of thinking about this is I want to cbind()
vectors into different (never overlapping) rows.
Try
library(reshape2)
library(data.table)
dcast(setDT(melt(lv))[, rn:=.I], rn~L1, value.var='value')
Or
dcast(setDT(melt(lv), keep.rownames=TRUE),
as.numeric(rn)~L1, value.var='value')
Or as suggested by @David Arenburg
recast(lv, seq_along(unlist(lv)) ~ L1)
Or using base R
d1 <- stack(lv)
reshape(transform(d1, rn=1:nrow(d1)), idvar='rn',
timevar='ind', direction='wide')
这篇关于如何将向量转换成不同的列,使NAs留在剩余的单元格中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!