我正在研究批处理excel工作表的代码,以导入到关系数据库中。每个excel工作表都代表不同大象家族的数据,其中包含一组个体及其在不同日期的有无。我需要将此代码用作通用代码,因为我每年有50多个工作表,并且要导入> 10年。
[家庭中的个体数量随观察日期的变化而变化。我需要转置每个小标题元素,以使我可以用单个代码替换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 *()collect()或t()的每次尝试都失败了。
“ mysheets”是50个小标题的列表,每个小标题一个,最大为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)
})
已转置会产生空结果-没有引发错误,但对象为空
有人可以帮助我了解如何处理列表中的每个小标题,以便我可以继续解决其余问题吗?谢谢
这个(X是您的问题的结构)?