R语言 full_join嵌套列表中的每个列表,只在一个大括号中使用lapply(不是单独的lapply或map2)

kknvjkwl  于 2023-06-19  发布在  其他
关注(0)|答案(1)|浏览(105)

我已经看过过去类似的问题,但没有一个真正像我的问题,所以我开始:

这是列表as
这是从一个文件夹结构(下面)获得的

使用下面的代码,我得到了read.csv部分并命名了嵌套列表

data_dir <- "C:/Users/thepr/Documents/data/as"
num_fu <- 1 : 9
dirs <- paste0(data_dir, num_fu)

as_list <- lapply(dirs, function(x) {
  files <- list.files(x, pattern = "\\.csv$", full.names = TRUE)
  names(files) <- str_sub(basename(files), 1, 6)
  lapply(files, read.csv
    })

我想做的是

>as[[1]]
(a full_join dataframe of as[[1]][[1]], as[[1]][[2]], as[[1]][[3]],... as[[1]][[9]])
>as[[2]]
(a full_join dataframe of as[[2]][[1]], as[[2]][[2]], as[[2]][[3]],... as[[2]][[9]])
.
.
.
>as[[9]]
(a full_join dataframe of as[[9]][[1]], as[[9]][[2]], as[[9]][[3]],... as[[9]][[9]])

每个连接列表的名称是

>names(as_list)
[[1]] as1
[[2]] as2
.
.
.
[[9]] as9

这个嵌套列表使得lapply的使用变得困难。正如这里所建议的,我想使用reduce(full_join,as_list x),但我不知道如何将其转换为R代码。

data_dir <- "C:/Users/thepr/Documents/data/as"
num_fu <- 1 : 9
dirs <- paste0(data_dir, num_fu)

as_list <- lapply(dirs, function(x) {
  files <- list.files(x, pattern = "\\.csv$", full.names = TRUE)
  names(files) <- str_sub(basename(files), 1, 6)
  lapply(files, function(y) {
    read.csv(y)
    reduce(full_join, as_list[[x]])
    })
})

Error in x[[1]] : object of type 'closure' is not subsettable
Called from: reduce_init(.x, .init, left = left, error_call = .purrr_error_call)

as_list <- lapply(as_list, function(x){
  reduce(full_join, as_list[[x]])
  })

但毫无结果。谢谢你!

5gfr0r5j

5gfr0r5j1#

data_dir <- "C:/Users/thepr/Documents/data/as"
num_fu <- 1 : 9
dataframe <- "as"
dirs <- paste0(data_dir, num_fu)
as <- data.frame()

as_list <- lapply(dirs, function(x) {
  files <- list.files(x, pattern = "\\.csv$", full.names = TRUE)
  names(files) <- str_sub(basename(files), 1, 6)
  Reduce(full_join, lapply(files, read.csv))
})

names(as_list) <- paste0(dataframe, num_fu)
as <- Reduce(full_join, as_list)

给予

> as_list[[1]] %>% head() %>% dput()
structure(list(RID = c("EPI22_039_181584", "EPI22_039_077150", 
"EPI22_039_042243", "EPI22_039_115383", "EPI22_039_035585", "EPI22_039_070773"
), AS1_AREA = c(1L, 1L, 1L, 1L, 2L, 1L), AS1_EDATE1 = c(200210L, 
200111L, 200108L, 200110L, 200201L, 200111L), AS1_SEX = c(1L, 
2L, 1L, 1L, 1L, 1L), AS1_AGE = c(63L, 60L, 49L, 62L, 46L, 66L
), AS1_FAMNUM = c(2L, 1L, 2L, 2L, 4L, 2L), AS1_HAND = c(3L, 1L, 
1L, 1L, 1L, 1L), AS1_SEAS = c(3L, 1L, 1L, 3L, 3L, 3L), AS1_MARRYA = c(2L, 
2L, 2L, 1L, 2L, 2L), AS1_MARRYAETC = c("77777", "77777", "77777", 
....

相关问题