R:循环合并数据帧并绑定它们
我有一个类似的问题 [] R:合并不相等的数据帧并将缺失的行替换为 0
以下是此问题的数据:
df1 = data.frame(x=c('a', 'b', 'c', 'd', 'e'))
df2 = data.frame(x=c('a', 'b', 'c'),y = c(0,1,0))
df3 = data.frame(x=c('a', 'b', 'c', 'd'),y = c(1,1,1,0))
df4 = data.frame(x=c('b', 'a', 'e'),y = c(0,1,0))
zz <- merge(df1, df2, all = TRUE)
zz[is.na(zz)] <- 0
在本例中,我将 df1 与 df2 合并。现在我想创建一个循环来合并 df3 和 df4 以及更多数据帧与 df1。问题是列表中的结果必须进行 cbind 才能生成最终的数据帧。
谁能帮助我吗?
谢谢!
编辑! 这是我创建的循环。变量 goterms 包含一个包含 10 个变量的列表。变量是interestedGO 中列表的名称。选择第一个感兴趣的GO,计算的结果就是变量result。该结果必须与 x 合并。因为它是一个循环,所以必须对所有 10 个结果进行 cbind 以创建最终的数据帧。
for (i in 1:length(goterms)){
goilmn<-as.data.frame(interestedGO[i])
resultILMN<-match(goilmn[,1], rownames(xx2),nomatch=0)
resultILMN[resultILMN] <- 1
result<-cbind(goilmn,resultILMN)
colnames(result) <- c('x','result')
zz<-merge(x, result, all=TRUE)
resultloop<-zz[is.na(zz)]<-0
standard[i]<-cbind(resultloop)
}
goterms:
[1] "GO:0009611" "GO:0007596" "GO:0050817" "GO:0061082" "GO:0007599"
[6] "GO:0050776" "GO:0006910" "GO:0034383" "GO:0019932" "GO:0002720"
interestedGO:
$`GO:0009611`
[1] "ILMN_1651346" "ILMN_1651354" "ILMN_1651599" "ILMN_1651950" "ILMN_1652287"
[6] "ILMN_1652445" "ILMN_1652693" "ILMN_1652825" "ILMN_1653324" "ILMN_1653395"
$`GO:0007596`
[1] "ILMN_1651599" "ILMN_1652693" "ILMN_1652825" "ILMN_1653324" "ILMN_1655595"
[6] "ILMN_1656057" "ILMN_1659077" "ILMN_1659923" "ILMN_1659947" "ILMN_1662619"
[11] "ILMN_1664565" "ILMN_1665132" "ILMN_1665859" "ILMN_1666175" "ILMN_1668052"
[16] "ILMN_1670229" "ILMN_1670305" "ILMN_1670490" "ILMN_1670708"
"ILMN_1671766"
$`GO:0050817`
[1] "ILMN_1651599" "ILMN_1652693" "ILMN_1652825" "ILMN_1653324" "ILMN_1655595"
[6] "ILMN_1656057" "ILMN_1659077" "ILMN_1659923" "ILMN_1659947" "ILMN_1662619"
[11] "ILMN_1664565" "ILMN_1665132" "ILMN_1665859" "ILMN_1666175" "ILMN_1668052"
[16] "ILMN_1670229" "ILMN_1670305" "ILMN_1670490" "ILMN_1670708" "ILMN_1671766"
[21] "ILMN_1671928" "ILMN_1675083" "ILMN_1678049" "ILMN_1678728"
"ILMN_1680805"
$`GO:0061082`
[1] "ILMN_1661695" "ILMN_1665132" "ILMN_1716446" "ILMN_1737314" "ILMN_1772387"
[6] "ILMN_1784863" "ILMN_1796094" "ILMN_1800317" "ILMN_1800512" "ILMN_1807074"
x 是所有 ILMN 代码的引用。这是 x 变量的头部。 x[1:100,]
[1] ILMN_1343291 ILMN_1343295 ILMN_1651228 ILMN_1651229 ILMN_1651238
[6] ILMN_1651254 ILMN_1651259 ILMN_1651260 ILMN_1651262 ILMN_1651278
[11] ILMN_1651282 ILMN_1651285 ILMN_1651286 ILMN_1651303 ILMN_1651310
[16] ILMN_1651315 ILMN_1651330 ILMN_1651336 ILMN_1651343 ILMN_1651346
[21] ILMN_1651347 ILMN_1651354 ILMN_1651358 ILMN_1651370 ILMN_1651373
[26] ILMN_1651385 ILMN_1651396 ILMN_1651415 ILMN_1651428 ILMN_1651430
[31] ILMN_1651433 ILMN_1651437 ILMN_1651438 ILMN_1651456 ILMN_1651457
I have a question like [] R: merge unequal dataframes and replace missing rows with 0
Here is the data for this question:
df1 = data.frame(x=c('a', 'b', 'c', 'd', 'e'))
df2 = data.frame(x=c('a', 'b', 'c'),y = c(0,1,0))
df3 = data.frame(x=c('a', 'b', 'c', 'd'),y = c(1,1,1,0))
df4 = data.frame(x=c('b', 'a', 'e'),y = c(0,1,0))
zz <- merge(df1, df2, all = TRUE)
zz[is.na(zz)] <- 0
In this example I merged df1 with df2. Now I want to create a loop to merged df3 and df4 and more dataframes with df1. The problem is that the results in the list must be cbind to generate a final dataframe.
Can anyone help me?
Thanks!
EDIT!
This is the loop I created. The variable goterms contrains a list with 10 variables. The variables are the names of the lists in the interestedGO. The first interestedGO is selected, and the result of calculation is the variable result. This result must be merged with x. Because it is a loop, all the 10 results must be cbind to create a final dataframe.
for (i in 1:length(goterms)){
goilmn<-as.data.frame(interestedGO[i])
resultILMN<-match(goilmn[,1], rownames(xx2),nomatch=0)
resultILMN[resultILMN] <- 1
result<-cbind(goilmn,resultILMN)
colnames(result) <- c('x','result')
zz<-merge(x, result, all=TRUE)
resultloop<-zz[is.na(zz)]<-0
standard[i]<-cbind(resultloop)
}
goterms:
[1] "GO:0009611" "GO:0007596" "GO:0050817" "GO:0061082" "GO:0007599"
[6] "GO:0050776" "GO:0006910" "GO:0034383" "GO:0019932" "GO:0002720"
interestedGO:
I have a question like [] R: merge unequal dataframes and replace missing rows with 0
Here is the data for this question:
df1 = data.frame(x=c('a', 'b', 'c', 'd', 'e'))
df2 = data.frame(x=c('a', 'b', 'c'),y = c(0,1,0))
df3 = data.frame(x=c('a', 'b', 'c', 'd'),y = c(1,1,1,0))
df4 = data.frame(x=c('b', 'a', 'e'),y = c(0,1,0))
zz <- merge(df1, df2, all = TRUE)
zz[is.na(zz)] <- 0
In this example I merged df1 with df2. Now I want to create a loop to merged df3 and df4 and more dataframes with df1. The problem is that the results in the list must be cbind to generate a final dataframe.
Can anyone help me?
Thanks!
EDIT!
This is the loop I created. The variable goterms contrains a list with 10 variables. The variables are the names of the lists in the interestedGO. The first interestedGO is selected, and the result of calculation is the variable result. This result must be merged with x. Because it is a loop, all the 10 results must be cbind to create a final dataframe.
GO:0009611`
[1] "ILMN_1651346" "ILMN_1651354" "ILMN_1651599" "ILMN_1651950" "ILMN_1652287"
[6] "ILMN_1652445" "ILMN_1652693" "ILMN_1652825" "ILMN_1653324" "ILMN_1653395"
I have a question like [] R: merge unequal dataframes and replace missing rows with 0
Here is the data for this question:
df1 = data.frame(x=c('a', 'b', 'c', 'd', 'e'))
df2 = data.frame(x=c('a', 'b', 'c'),y = c(0,1,0))
df3 = data.frame(x=c('a', 'b', 'c', 'd'),y = c(1,1,1,0))
df4 = data.frame(x=c('b', 'a', 'e'),y = c(0,1,0))
zz <- merge(df1, df2, all = TRUE)
zz[is.na(zz)] <- 0
In this example I merged df1 with df2. Now I want to create a loop to merged df3 and df4 and more dataframes with df1. The problem is that the results in the list must be cbind to generate a final dataframe.
Can anyone help me?
Thanks!
EDIT!
This is the loop I created. The variable goterms contrains a list with 10 variables. The variables are the names of the lists in the interestedGO. The first interestedGO is selected, and the result of calculation is the variable result. This result must be merged with x. Because it is a loop, all the 10 results must be cbind to create a final dataframe.
GO:0007596`
[1] "ILMN_1651599" "ILMN_1652693" "ILMN_1652825" "ILMN_1653324" "ILMN_1655595"
[6] "ILMN_1656057" "ILMN_1659077" "ILMN_1659923" "ILMN_1659947" "ILMN_1662619"
[11] "ILMN_1664565" "ILMN_1665132" "ILMN_1665859" "ILMN_1666175" "ILMN_1668052"
[16] "ILMN_1670229" "ILMN_1670305" "ILMN_1670490" "ILMN_1670708"
"ILMN_1671766"
I have a question like [] R: merge unequal dataframes and replace missing rows with 0
Here is the data for this question:
df1 = data.frame(x=c('a', 'b', 'c', 'd', 'e'))
df2 = data.frame(x=c('a', 'b', 'c'),y = c(0,1,0))
df3 = data.frame(x=c('a', 'b', 'c', 'd'),y = c(1,1,1,0))
df4 = data.frame(x=c('b', 'a', 'e'),y = c(0,1,0))
zz <- merge(df1, df2, all = TRUE)
zz[is.na(zz)] <- 0
In this example I merged df1 with df2. Now I want to create a loop to merged df3 and df4 and more dataframes with df1. The problem is that the results in the list must be cbind to generate a final dataframe.
Can anyone help me?
Thanks!
EDIT!
This is the loop I created. The variable goterms contrains a list with 10 variables. The variables are the names of the lists in the interestedGO. The first interestedGO is selected, and the result of calculation is the variable result. This result must be merged with x. Because it is a loop, all the 10 results must be cbind to create a final dataframe.
GO:0050817`
[1] "ILMN_1651599" "ILMN_1652693" "ILMN_1652825" "ILMN_1653324" "ILMN_1655595"
[6] "ILMN_1656057" "ILMN_1659077" "ILMN_1659923" "ILMN_1659947" "ILMN_1662619"
[11] "ILMN_1664565" "ILMN_1665132" "ILMN_1665859" "ILMN_1666175" "ILMN_1668052"
[16] "ILMN_1670229" "ILMN_1670305" "ILMN_1670490" "ILMN_1670708" "ILMN_1671766"
[21] "ILMN_1671928" "ILMN_1675083" "ILMN_1678049" "ILMN_1678728"
"ILMN_1680805"
I have a question like [] R: merge unequal dataframes and replace missing rows with 0
Here is the data for this question:
df1 = data.frame(x=c('a', 'b', 'c', 'd', 'e'))
df2 = data.frame(x=c('a', 'b', 'c'),y = c(0,1,0))
df3 = data.frame(x=c('a', 'b', 'c', 'd'),y = c(1,1,1,0))
df4 = data.frame(x=c('b', 'a', 'e'),y = c(0,1,0))
zz <- merge(df1, df2, all = TRUE)
zz[is.na(zz)] <- 0
In this example I merged df1 with df2. Now I want to create a loop to merged df3 and df4 and more dataframes with df1. The problem is that the results in the list must be cbind to generate a final dataframe.
Can anyone help me?
Thanks!
EDIT!
This is the loop I created. The variable goterms contrains a list with 10 variables. The variables are the names of the lists in the interestedGO. The first interestedGO is selected, and the result of calculation is the variable result. This result must be merged with x. Because it is a loop, all the 10 results must be cbind to create a final dataframe.
GO:0061082`
[1] "ILMN_1661695" "ILMN_1665132" "ILMN_1716446" "ILMN_1737314" "ILMN_1772387"
[6] "ILMN_1784863" "ILMN_1796094" "ILMN_1800317" "ILMN_1800512" "ILMN_1807074"
x is a reference of all ILMN code. Here is an head of the x variable.
x[1:100,]
[1] ILMN_1343291 ILMN_1343295 ILMN_1651228 ILMN_1651229 ILMN_1651238
[6] ILMN_1651254 ILMN_1651259 ILMN_1651260 ILMN_1651262 ILMN_1651278
[11] ILMN_1651282 ILMN_1651285 ILMN_1651286 ILMN_1651303 ILMN_1651310
[16] ILMN_1651315 ILMN_1651330 ILMN_1651336 ILMN_1651343 ILMN_1651346
[21] ILMN_1651347 ILMN_1651354 ILMN_1651358 ILMN_1651370 ILMN_1651373
[26] ILMN_1651385 ILMN_1651396 ILMN_1651415 ILMN_1651428 ILMN_1651430
[31] ILMN_1651433 ILMN_1651437 ILMN_1651438 ILMN_1651456 ILMN_1651457
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定我是否正确理解你想要什么,但是像这样?
您可以通过使用Reduce来避免循环,但请注意,这并不一定会带来性能提升。
如果你想要单独的数据帧,那么 Map (只是 mapply 的包装器)很有用:
并通过 do.call cbind 它们
I'm not sure if I understand correctly what you want, but like this?
You can avoid loop by using Reduce, but note that it does not necessarily lead to performance improvement.
If you want separate dataframes, then Map (just the wrapper of mapply) is useful:
and cbind them by do.call