r语言 - 收集不同长度的 tibbles 列表 - a for 循环?



我正在处理批处理 excel 工作表的代码,以便导入到关系数据库中。每个 Excel 工作表代表不同大象家族的数据,其中包含一组个体及其在不同日期的存在/缺席。我需要这是可推广的代码,因为我每年有 50+ 张纸,> 10 年要导入。

一个家庭中个体的数量各不相同,观察他们的日期数量也各不相同。我需要转置每个 tibble 元素,以允许我用单独的代码替换 1(已经在 StackOverflow 上回答(,然后我可以为每个系列重新收集到一个列表中,如下所示。

数据当前在Excel中;

Ind    Date1 Date2 Date3 Date4              
A        1    1           1
B        1    1
C        1           1
D        1           1

我正在努力让它;

Date1   A
Date1   B
Date1   C
Date1   D
Date2   A
Date2   B
Date3   C
Date3   D
Date4   A

我想我需要一个 for 循环来做到这一点,因为每个元素的长度都不同,所以我对 map*(( gather(( 或 t(( 的每一次努力都失败了。

"Mysheets"是一个包含 50 个 tibble 的列表,每个系列一个,其中最大的是 60 行和 93 列;一个例子

dput(head(mysheets, 4((

'list(AA = structure(list(Date = c("Famsize", "Grpsize", "ALY68", 
"AME16", "AME12", "AME99", "AME90", "ANN12", "ANN03", "ALF16", 
"AME81", "ANH16", "ANH11", "ALI79", "AST97", "ALI98", "ART14", 
"ART10", "ALI02", "ARD13", "ALI12", "AGA82", "ALT14", "ALT02", 
"AGA93", "ALX15", "ALX11", "AMY85", "ANG15", "ANG11", "AMB10", 
"AUD94", "ABR12", "ART17"), `42761` = c(4, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, 1, 1, 1, 1, NA, NA, NA), `42767` = c(12, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, 1, 1, 1, 
1, 1, 1, NA, NA, NA, NA, NA, NA, 1, 1, 1, 1, NA, NA, NA), `42770` = c(15, 
NA, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, NA, NA, 1, 1, 1, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, NA), `42773` = c(20, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, NA), `42777` = c(6, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, 1, 1, 1, 1, NA), 
`42782` = c(6, 7, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, 1, 1, 1, 1, NA, 
NA, NA, NA, NA, NA, NA), `42802...8` = c(6, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, 1, 1, 1, 1, 1, 1, NA), `42802...9` = c(8, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, 1, 
1, 1, 1, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA), `42809` = c(3, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 1, 1, 1, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA), `42816` = c(22, NA, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, NA, NA, NA, 1, 1, 
1, NA, NA, NA, NA, NA, NA, 1, 1, 1, 1, 1, 1, NA), `42850...12` = c(8, 
NA, 1, 1, 1, 1, NA, NA, NA, 1, 1, 1, 1, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA), `42850...13` = c(14, 16, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 1, 1, 1, 1, 1, 1, 1, 1, NA, NA, NA, NA, NA, 
NA, 1, 1, 1, 1, 1, 1, NA), `42859` = c(2, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), 
`42860...15` = c(2, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 1, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA), `42860...16` = c(6, 14, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, 1, NA, 1, 1, 
NA), `42862` = c(8, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, 1, 1, NA, NA, NA, 1, 1, 1, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, 1, 1, 1, NA), `42864` = c(3, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, 1, 1, 1, NA, NA, NA, NA), `42866` = c(6, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 
1, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, 1, NA, NA, 
NA, NA), `42870` = c(8, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, 1, NA, NA, NA, 1, 1, 1, 1, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, 1, 1, NA), `42880` = c(6, 11, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, 1, 1, 1, NA, NA, 
1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), `42784...22` = c(8, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, 1, 
1, 1, 1, 1, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA), `42784...23` = c(2, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, NA), `42823` = c(8, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 
1, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, 1, NA, 1, 
1, NA), `42817` = c(6, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, 1, 1, 1, 
1, NA, NA, NA, NA, NA, NA, NA), `42896` = c(6, 16, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, 1, 1, 1, 1, NA), `42933...27` = c(14, 
27, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, 1, 
1, 1, 1, 1, 1, NA, NA, NA, NA, NA, NA, 1, 1, 1, 1, 1, 1, 
NA), `43057` = c(7, NA, NA, NA, NA, 1, 1, 1, 1, NA, 1, NA, 
1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA), `43082` = c(7, NA, NA, NA, NA, 
1, 1, 1, 1, NA, 1, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), `42928` = c(7, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, 
NA, NA, 1, 1, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, 
1, NA), `42933...31` = c(11, 24, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, 1, 1, NA, NA, NA, 1, 1, 1, NA, NA, NA, 
NA, NA, NA, 1, 1, 1, 1, 1, 1, NA), `42935...32` = c(3, 21, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 
1, NA), `42935...33` = c(4, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, 1, 1, 1, 1, NA, NA, NA), `42936` = c(11, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, NA, NA, 
NA, 1, 1, 1, NA, NA, NA, NA, NA, NA, 1, 1, 1, 1, 1, 1, NA
), `42949...35` = c(4, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, 1, 1, NA), `42949...36` = c(3, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, 
1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA), `42952` = c(2, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA), `43319` = c(5, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, 1, NA, 1, 1, NA
), `42959...39` = c(6, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, 1, 1, 1, 
1, NA, NA, NA, NA, NA, NA, NA), `42959...40` = c(10, NA, 
1, NA, 1, 1, 1, 1, 1, 1, 1, 1, 1, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
), `42966` = c(4, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 1, 1, 1, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 1, NA, NA, NA), `42978` = c(10, NA, 1, NA, 
1, 1, 1, 1, 1, 1, 1, 1, 1, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), `42986` = c(2, 
NA, 1, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA), `42992...44` = c(6, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, 1, 1, 1, 1, 1, 1, NA), `42992...45` = c(3, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 
1, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA), `42997` = c(6, 10, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, 1, 
1, 1, 1, NA, NA, NA, NA, NA, NA, NA), `43007` = c(3, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, NA, NA, 
NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA), `43015` = c(6, 7, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, NA, 1, 1, 1, 
1, NA, NA, NA, NA, NA, NA, NA), `43046` = c(3, 14, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, 1, NA, NA, NA, NA), 
`41222` = c(3, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, 1, 
NA, NA, NA, NA, NA, NA, NA), `43048...51` = c(5, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, 1, 1, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA
), `43048...52` = c(3, 7, NA, NA, 1, NA, NA, NA, NA, NA, 
1, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA), `43054` = c(5, 10, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
1, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, NA
), `43068` = c(3, 6, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, 1, 1, 1, NA, NA, NA, NA), `43073` = c(8, 10, NA, NA, 
1, 1, 1, 1, NA, NA, 1, NA, 1, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), 
`43076...56` = c(12, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, NA, 1, 1, 1, 
1, 1, 1, 1, NA, 1, 1, NA), `43076...57` = c(2, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, 1, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
), `43085...58` = c(3, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, 1, 1, 1, NA, NA, NA, NA), `43085...59` = c(6, NA, 
NA, NA, 1, 1, 1, 1, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA), `43092...60` = c(3, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, 1, NA, 1, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, 1), `43092...61` = c(8, 
9, NA, NA, 1, 1, 1, 1, 1, NA, 1, NA, 1, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA), `43093` = c(15, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, 1, NA, 1, 1, 1, 1, 1, NA, 1, 1, 1, 1, 
NA, NA, NA, 1, 1, 1, 1), `43099` = c(8, 26, NA, NA, 1, 1, 
1, 1, 1, NA, 1, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), row.names = c(NA, 
-34L), class = c("tbl_df", "tbl", "data.frame")), AC = structure(list(
Date = c("Famsize", "Grpsize", "WAR67", "ABI13", "ABI05", 
"AGA93", "AXA17", "AXA13", "ABI82", "ANW15", "ANW10", "WAR79", 
"ANA12"), `42880` = c(2, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, 1, 1), `42888` = c(6, 14, NA, 1, NA, 1, NA, 1, 1, 
1, 1, NA, NA), `42978...4` = c(3, 5, NA, NA, NA, NA, NA, 
NA, 1, 1, 1, NA, NA), `42978...5` = c(3, 7, NA, 1, NA, 1, 
NA, 1, NA, NA, NA, NA, NA), `42997` = c(6, 8, NA, 1, NA, 
1, NA, 1, 1, 1, 1, NA, NA), `43007` = c(3, 4, NA, NA, NA, 
NA, NA, NA, 1, 1, 1, NA, NA), `43025` = c(6, 11, NA, 1, NA, 
1, NA, 1, 1, 1, 1, NA, NA), `43069` = c(2, 9, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, 1, 1), `43081` = c(3, NA, NA, NA, 
NA, 1, 1, 1, NA, NA, NA, NA, NA), `43083` = c(4, NA, NA, 
1, NA, 1, 1, 1, NA, NA, NA, NA, NA), `43087` = c(4, 6, NA, 
1, NA, NA, NA, NA, 1, 1, 1, NA, NA), `43092` = c(3, 17, NA, 
NA, NA, NA, NA, NA, 1, 1, 1, NA, NA), `43096` = c(7, NA, 
NA, 1, NA, 1, 1, 1, 1, 1, 1, NA, NA), `43057` = c(4, 8, NA, 
1, NA, NA, NA, NA, 1, 1, 1, NA, NA), `43082...16` = c(4, 
NA, NA, 1, NA, 1, 1, 1, NA, NA, NA, NA, NA), `43082...17` = c("4", 
"6", NA, NA, "?", NA, NA, NA, "1", "1", "1", NA, NA)), row.names = c(NA, 
-13L), class = c("tbl_df", "tbl", "data.frame")), BB = structure(list(
Date = c("Famsize", "Grpsize", "BAR", "BAR01", "BDU14", "BAR87", 
"BEC16", "BEC11", "BON83", "BRL11", "BON01", "BOL15", "BON93", 
"BIL16", "BIL12", "BEV90", "BAA12", "BAA03", "BEV97", "BOD12", 
"BRN96", "BLL15", "BLL10", "BEA00", "BEL87", "BOG16", "BOG11", 
"BOG04", "Extra12F"), `42943` = c(3, NA, NA, NA, NA, 1, 1, 
1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA), `43001` = c(9, 10, 1, 1, 1, 
1, 1, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
1, 1, 1, NA, NA, NA, NA, NA, NA), `43008` = c(14, 16, 1, 
1, 1, 1, 1, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, 1, 1, 1, 1, 1, 1, 1, 1, 1)), row.names = c(NA, -29L), class = c("tbl_df", 
"tbl", "data.frame")), BB2 = structure(list(Date = c("Famsize", 
"Grpsize", "BET70", "BNT12", "BNT05", "BNT83", "BRY15", "BRY11"
), `42761` = c(6, 17, 1, 1, 1, 1, 1, 1), `42786` = c(6, 7, 1, 
1, 1, 1, 1, 1), `42865` = c(6, NA, 1, 1, 1, 1, 1, 1), `42866` = c(6, 
NA, 1, 1, 1, 1, 1, 1), `42871` = c(6, NA, 1, 1, 1, 1, 1, 1), 
`42944` = c(6, 10, 1, 1, 1, 1, 1, 1), `43099` = c(6, NA, 
1, 1, 1, 1, 1, 1)), row.names = c(NA, -8L), class = c("tbl_df", 
"tbl", "data.frame")))

transposed <- as.list(for(family in mysheets$family){
gather(family, na.rm = FALSE)
})

转置生成空结果 - 不抛出错误,但对象为空

谁能帮助我了解如何转置列表中的每个 tibble,以便我可以继续处理其余的问题?谢谢

这个怎么样(你的问题的结构在哪里X(?

我敢肯定它可以变得更加精致(map(,但这里是:

library(tidyverse)
AA <- X[[1]]
AC <- X[[2]]
BB <- X[[3]]
BB2 <- X[[4]]
data_new <- function(data, tag){
data %>% 
filter(!Date %in% c('Famsize', 'Grpsize')) %>% 
rename('EleID' = Date) %>% 
gather(key = 'Date', value = 'Value', -EleID) %>% 
filter(!is.na(Value)) %>% 
select(-Value) %>% 
mutate(dataset = tag)
} 
AA_new <- data_new(AA, "AA")
AC_new <- data_new(AC, "AC")
BB_new <- data_new(BB, "BB")
BB2_new <- data_new(BB2, "BB")
data_combined <- bind_rows(AA_new, AC_new, BB_new, BB2_new)

。生成:

glimpse(data_combined)
Observations: 537
Variables: 3
$ EleID   <chr> "AMY85", "ANG15", "ANG11", "AMB10", "ALI79", "AST97", "ALI98…
$ Date    <chr> "42761", "42761", "42761", "42761", "42767", "42767", "42767…
$ dataset <chr> "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", …

而且(正如您所说,经过一番清洁(我认为您可以使用包janitor中的excel_numeric_to_date函数mutate,以便从Excel版本中获取R类型日期。

我希望这对您有所帮助。

相关内容

  • 没有找到相关文章

最新更新