R:循环合并数据帧并绑定它们

发布于 2024-11-07 05:32:53 字数 3022 浏览 1 评论 0原文

我有一个类似的问题 [] 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

归途 2024-11-14 05:32:53

我不确定我是否正确理解你想要什么,但是像这样?

> zz <- Reduce(function(a,b)merge(a,b,all=TRUE, by="x"), list(df1, df2, df3, df4))
> zz[is.na(zz)] <- 0
> zz
  x y.x y.y y
1 a   0   1 1
2 b   1   1 0
3 c   0   1 0
4 d   0   0 0
5 e   0   0 0

您可以通过使用Reduce来避免循环,但请注意,这并不一定会带来性能提升。

如果你想要单独的数据帧,那么 Map (只是 mapply 的包装器)很有用:

> zz <- Map(function(b)merge(df1,b,all=TRUE, by="x"), list(df2, df3, df4))
> zz
[[1]]
  x  y
1 a  0
2 b  1
3 c  0
4 d NA
5 e NA

[[2]]
  x  y
1 a  1
2 b  1
3 c  1
4 d  0
5 e NA

[[3]]
  x  y
1 a  1
2 b  0
3 c NA
4 d NA
5 e  0

并通过 do.call cbind 它们

> zz <- do.call("cbind", zz)
> zz[is.na(zz)] <- 0
> zz
  x y x y x y
1 a 0 a 1 a 1
2 b 1 b 1 b 0
3 c 0 c 1 c 0
4 d 0 d 0 d 0
5 e 0 e 0 e 0

I'm not sure if I understand correctly what you want, but like this?

> zz <- Reduce(function(a,b)merge(a,b,all=TRUE, by="x"), list(df1, df2, df3, df4))
> zz[is.na(zz)] <- 0
> zz
  x y.x y.y y
1 a   0   1 1
2 b   1   1 0
3 c   0   1 0
4 d   0   0 0
5 e   0   0 0

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:

> zz <- Map(function(b)merge(df1,b,all=TRUE, by="x"), list(df2, df3, df4))
> zz
[[1]]
  x  y
1 a  0
2 b  1
3 c  0
4 d NA
5 e NA

[[2]]
  x  y
1 a  1
2 b  1
3 c  1
4 d  0
5 e NA

[[3]]
  x  y
1 a  1
2 b  0
3 c NA
4 d NA
5 e  0

and cbind them by do.call

> zz <- do.call("cbind", zz)
> zz[is.na(zz)] <- 0
> zz
  x y x y x y
1 a 0 a 1 a 1
2 b 1 b 1 b 0
3 c 0 c 1 c 0
4 d 0 d 0 d 0
5 e 0 e 0 e 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文