cbind 有条件的不同拆分行
cbind different splited rows with conditional
我想 cbind 有条件的数据帧以在后续行中插入 "NA"。我在数据帧 dfall
中应用 split
并得到:
我的df[[1]]
:
chr start end N01 N04 N05
1 30 40 30 20 29
我的df[[2]]
:
chr start end N01 N02 N03
2 40 50 30 20 29
我想得到:
chr start end N01.a N04 N05 N01.b N02 N03
1 30 40 30 20 29 NA NA NA
2 40 50 NA NA NA 30 20 29
我试了没有成功:
labelallgenes <- dfall[1:2,]
dfall.split<- split(dfall, 1:nrow(dfall))
for(k in length(dfall.split)){
dfk <- dfall.split[[k]]
labelallgenes <- cbind.fill(labelallgenes, dfk, fill=NA)}
有什么想法吗?
非常感谢。
假设'df'是一个列表,我们可以把'Reduce'和'merge'两个列表元素一起使用
Reduce(function(...) merge(..., by=c('chr', 'start', 'end'), all=TRUE), df)
# chr start end N01.x N04 N05 N01.y N02 N03
#1 1 30 40 30 20 29 NA NA NA
#2 2 40 50 NA NA NA 30 20 29
数据
df <- list(structure(list(chr = 1L, start = 30L, end = 40L, N01 = 30L,
N04 = 20L, N05 = 29L), .Names = c("chr", "start", "end",
"N01", "N04", "N05"), class = "data.frame", row.names = c(NA,
-1L)), structure(list(chr = 2L, start = 40L, end = 50L, N01 = 30L,
N02 = 20L, N03 = 29L), .Names = c("chr", "start", "end",
"N01", "N02", "N03"), class = "data.frame", row.names = c(NA, -1L)))
我想 cbind 有条件的数据帧以在后续行中插入 "NA"。我在数据帧 dfall
中应用 split
并得到:
我的df[[1]]
:
chr start end N01 N04 N05
1 30 40 30 20 29
我的df[[2]]
:
chr start end N01 N02 N03
2 40 50 30 20 29
我想得到:
chr start end N01.a N04 N05 N01.b N02 N03
1 30 40 30 20 29 NA NA NA
2 40 50 NA NA NA 30 20 29
我试了没有成功:
labelallgenes <- dfall[1:2,]
dfall.split<- split(dfall, 1:nrow(dfall))
for(k in length(dfall.split)){
dfk <- dfall.split[[k]]
labelallgenes <- cbind.fill(labelallgenes, dfk, fill=NA)}
有什么想法吗? 非常感谢。
假设'df'是一个列表,我们可以把'Reduce'和'merge'两个列表元素一起使用
Reduce(function(...) merge(..., by=c('chr', 'start', 'end'), all=TRUE), df)
# chr start end N01.x N04 N05 N01.y N02 N03
#1 1 30 40 30 20 29 NA NA NA
#2 2 40 50 NA NA NA 30 20 29
数据
df <- list(structure(list(chr = 1L, start = 30L, end = 40L, N01 = 30L,
N04 = 20L, N05 = 29L), .Names = c("chr", "start", "end",
"N01", "N04", "N05"), class = "data.frame", row.names = c(NA,
-1L)), structure(list(chr = 2L, start = 40L, end = 50L, N01 = 30L,
N02 = 20L, N03 = 29L), .Names = c("chr", "start", "end",
"N01", "N02", "N03"), class = "data.frame", row.names = c(NA, -1L)))