【问题标题】:how to merge rows from dataframes contained in lists in r如何合并r中列表中包含的数据帧中的行
【发布时间】:2020-07-08 11:48:22
【问题描述】:

我有 3 个数据框列表,以这种方式列出 x、y 和 z

x
[1]
ID   1h   2h  3h
1    3     5  6
2    1     8  2
3    7     3  9
[2]
ID   1h   2h  3h
1    9     0  0
2    0     3  1
3    2     4  7
[3]
ID   1h   2h  3h
1    3     1  0
2    5     5  0
3    7     0  0
y
[1]
ID   1h   2h  3h
1    7     3  0
2    1     8  2
3    7     3  9
[2]
ID   1h   2h  3h
1    8     0  1
2    9     5  3
3    2     4  7
[3]
ID   1h   2h  3h
1    0     2  9
2    0     5  9
3    3     5  4
z
[1]
ID   1h   2h  3h
1    8     7  5
2    4     0  3
3    0     0  0
[2]
ID   1h   2h  3h
1    4     5  9
2    0     9  5
3    3     7  9
[3]
ID   1h   2h  3h
1    8     0  4
2    7     4  2
3    1     4  9

我希望结果采用 x 的第一个数据帧的 ID 并将其与第一个 y 数据帧的第一个 ID 和第一个 z 数据帧的第一个 ID 合并,看起来像这样,然后执行此操作对于列表中的所有数据框

ID1 [1]
      x     y  z
1h    3     7  8
2h    5     3  7
3h    6     0  5

为了澄清更多,这里是每个列表的第三个数据框的第二个 ID 的示例

ID2 [3]
      x     y  z
1h    5     0  7
2h    5     5  4
3h    0     9  2

最后还将它们导出到 csv 文件中,名称包含 ID 号(1,2 或 3)以及列表中的哪个项目([1]、[2] 或 [3]) 谢谢!

【问题讨论】:

标签: r list dataframe


【解决方案1】:

您可以尝试此解决方案。我使用列表来达到你想要的:

library(reshape2)
#Data
L1 <- list(structure(list(ID = 1:3, `1h` = c(3L, 1L, 7L), `2h` = c(5L, 
8L, 3L), `3h` = c(6L, 2L, 9L)), class = "data.frame", row.names = c(NA, 
-3L)), structure(list(ID = 1:3, `1h` = c(9L, 0L, 2L), `2h` = c(0L, 
3L, 4L), `3h` = c(0L, 1L, 7L)), class = "data.frame", row.names = c(NA, 
-3L)), structure(list(ID = 1:3, `1h` = c(3L, 5L, 7L), `2h` = c(1L, 
5L, 0L), `3h` = c(0L, 0L, 0L)), class = "data.frame", row.names = c(NA, 
-3L)))
L2 <- list(structure(list(ID = 1:3, `1h` = c(7L, 1L, 7L), `2h` = c(3L, 
8L, 3L), `3h` = c(0L, 2L, 9L)), class = "data.frame", row.names = c(NA, 
-3L)), structure(list(ID = 1:3, `1h` = c(8L, 9L, 2L), `2h` = c(0L, 
5L, 4L), `3h` = c(1L, 3L, 7L)), class = "data.frame", row.names = c(NA, 
-3L)), structure(list(ID = 1:3, `1h` = c(0L, 0L, 3L), `2h` = c(2L, 
5L, 5L), `3h` = c(9L, 9L, 4L)), class = "data.frame", row.names = c(NA, 
-3L)))
L3 <- list(structure(list(ID = 1:3, `1h` = c(7L, 1L, 7L), `2h` = c(3L, 
8L, 3L), `3h` = c(0L, 2L, 9L)), class = "data.frame", row.names = c(NA, 
-3L)), structure(list(ID = 1:3, `1h` = c(8L, 9L, 2L), `2h` = c(0L, 
5L, 4L), `3h` = c(1L, 3L, 7L)), class = "data.frame", row.names = c(NA, 
-3L)), structure(list(ID = 1:3, `1h` = c(0L, 0L, 3L), `2h` = c(2L, 
5L, 5L), `3h` = c(9L, 9L, 4L)), class = "data.frame", row.names = c(NA, 
-3L)))

#Format function
formatf <- function(x)
{
  y <- reshape2::melt(x,id.vars = 'ID')
  return(y)
}
#Apply
M1 <- lapply(L1,formatf)
M2 <- lapply(L2,formatf)
M3 <- lapply(L3,formatf)
#Merge function
fmerge <- function(a,b,c)
{
  d1 <- merge(a,b,by=c('ID','variable'))
  d2 <- merge(d1,c,by=c('ID','variable'))
  return(d2)
}
#Apply
LL=mapply(fmerge,M1,M2,M3,SIMPLIFY = FALSE)
#Format to export
names(LL)<-paste0('Group',1:length(LL))
#Bind all
DF <- do.call(rbind,LL)
#Assign group and format id
DF$Group <- rownames(DF)
DF <- DF[,c(6,1:5)]
rownames(DF)<-NULL
DF$Group <- gsub("\\..*","",DF$Group)
#Write to csv
write.csv(DF,file='DF.csv')

你会得到这个。我希望这会有所帮助。

    Group ID variable value.x value.y value
1  Group1  1       1h       3       7     7
2  Group1  1       2h       5       3     3
3  Group1  1       3h       6       0     0
4  Group1  2       1h       1       1     1
5  Group1  2       2h       8       8     8
6  Group1  2       3h       2       2     2
7  Group1  3       1h       7       7     7
8  Group1  3       2h       3       3     3
9  Group1  3       3h       9       9     9
10 Group2  1       1h       9       8     8
11 Group2  1       2h       0       0     0
12 Group2  1       3h       0       1     1
13 Group2  2       1h       0       9     9
14 Group2  2       2h       3       5     5
15 Group2  2       3h       1       3     3
16 Group2  3       1h       2       2     2
17 Group2  3       2h       4       4     4
18 Group2  3       3h       7       7     7
19 Group3  1       1h       3       0     0
20 Group3  1       2h       1       2     2
21 Group3  1       3h       0       9     9
22 Group3  2       1h       5       0     0
23 Group3  2       2h       5       5     5
24 Group3  2       3h       0       9     9
25 Group3  3       1h       7       3     3
26 Group3  3       2h       0       5     5
27 Group3  3       3h       0       4     4

【讨论】:

    【解决方案2】:

    如果我正确理解了您的问题,这应该可以解决问题。虽然不是最优雅的解决方案!

    # function to extract the id's
    extract_id <- function(x, y, z, id) {
      x <- x[x$ID == id, -1]
      y <- y[y$ID == id, -1]
      z <- z[z$ID == id, -1]
      
      id <- t(rbind(x, y, z))
      
      colnames(id) <- c("x", "y", "z")
      
      return(id)
    }
    
    # initialize new lists
    ID1 <- list()
    ID2 <- list()
    ID3 <- list()
    
    # loop over the data frames
    for (a in 1:3) {
      ID1[[a]] <- extract_id(x = x[[a]],
                             y = y[[a]],
                             z = z[[a]],
                             id = 1)
      ID2[[a]] <- extract_id(x = x[[a]],
                             y = y[[a]],
                             z = z[[a]],
                             id = 2)
      ID3[[a]] <- extract_id(x = x[[a]],
                             y = y[[a]],
                             z = z[[a]],
                             id = 3)
    }
    
    # show everything
    ID1
    ID2
    Id3
    

    【讨论】:

      猜你喜欢
      • 2020-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-19
      • 2016-05-07
      • 1970-01-01
      • 2015-04-26
      • 2013-06-14
      相关资源
      最近更新 更多