R语言 如何通过字符串识别函数使用 lapply 将新列添加到嵌套列表中



我正在尝试使用 %in% 函数将特定列添加到嵌套在列表列表中的数据框中。下面是我的数据的玩具示例。

dput(head(list)):

list(FEB_games = list(GAME1 = structure(list(GAME1_Class = c("paladin", 
"fighter", "wizard", "sorcerer", "rouge"), GAME1_Race = c("human", 
"elf", "orc", "human", "gnome"), GAME1_Alignment = c("NE", "CG", 
"CE", "NN", "LG"), GAME1_Level = c(6, 7, 6, 7, 7), GAME1_Alive = c("y", 
"y", "y", "y", "y")), row.names = c("m.Stan", "m.Kenny", "m.Cartman", 
"m.Kyle", "m.Butters"), class = "data.frame"), GAME2 = structure(list(
GAME2_Class = c("wizard", "cleric", "monk", "bard"), GAME2_Race = c("half-elf", 
"elf", "human", "dwarf"), GAME2_Alignment = c("CG", "CE", 
"NN", "LG"), GAME2_Level = c(5, 5, 5, 5), GAME2_Alive = c("y", 
"y", "y", "y")), row.names = c("m.Kenny", "m.Cartman", "m.Kyle", 
"m.Butters"), class = "data.frame")), MAR_games = list(GAME3 = structure(list(
GAME3_Class = c("cleric", "barbarian", "warlock", "monk"), 
GAME3_Race = c("elf", "half-elf", "elf", "dwarf"), GAME3_Alignment = c("LG", 
"LG", "CE", "LG"), GAME3_Level = c(1, 1, 1, 1), GAME3_Alive = c("y", 
"y", "y", "y")), row.names = c("l.Stan", "l.Kenny", "l.Cartman", 
"l.Butters"), class = "data.frame"), GAME4 = structure(list(GAME4_Class = c("fighter", 
"wizard", "sorcerer", "rouge"), GAME4_Race = c("half-elf", "elf", 
"human", "dwarf"), GAME4_Alignment = c("CG", "CE", "LN", "LG"
), GAME4_Level = c(5, 5, 5, 5), GAME4_Alive = c("y", "y", "y", 
"y")), row.names = c("l.Kenny", "l.Cartman", "l.Kyle", "l.Butters"), class = "data.frame")))

我有两组不同的列(数据框)要添加。 Feb_detentions添加到Feb_games和Mar_detentions添加到Mar_games。

dput(head(Feb_detentions)):

structure(list(Pupil = c("m.Stan", "m.Stan", "m.Kenny", "m.Cartman", 
"m.Kyle", "Butters"), Detention = c("y", "y", "y", "n", "n", "y"
)), row.names = c(NA, 6L), class = "data.frame")

dput(head(Mar_detentions)):

structure(list(Pupil = c("l.Stan", "l.Kenny", "l.Cartman", "l.Kyle"), 
Detention = c("n", "y", "y", "n")), row.names = c(NA, 4L), class = "data.frame")

我已经成功地使用这些步骤将感兴趣的列添加到数据框(未嵌套在列表中)。必须删除重复项的功能,我无法在函数内执行此操作。

Feb_detentions[! 重复(Feb_detentions$瞳孔),] -> Feb_detentions_dup

addDetentions <- function(df, df_namecol, detentions,  detention_namecol){
df[which(df_namecol %in% detention_namecol == T),] -> df_v1
detentions[which(detention_namecol %in% df_namecol == T),] -> det_v1
cbind(df_v1, det_v1) -> df_edit
return(df_edit)
}
addDetentions(df = GAME1, df_namecol = rownames(GAME1),
detentions = Feb_detentions_dup, detention_namecol = Feb_detentions_dup$Pupil) -> output

DPT(头(输出)):

structure(list(GAME1_Class = c("paladin", "fighter", "wizard", 
"sorcerer", "rouge"), GAME1_Race = c("human", "elf", "orc", "human", 
"gnome"), GAME1_Alignment = c("NE", "CG", "CE", "NN", "LG"), 
GAME1_Level = c(6, 7, 6, 7, 7), GAME1_Alive = c("y", "y", 
"y", "y", "y"), Pupil = c("m.Stan", "m.Kenny", "m.Cartman", "m.Kyle", 
"m.Butters"), Detention = c("y", "y", "n", "n", "y")), row.names =  c("m.Stan", "m.Kenny", "m.Cartman", "m.Kyle", "m.Butters"), class = "data.frame")

我想对整个列表执行此功能(或其他有效功能)。但是由于有两组不同的列要添加到单个列表中的两个不同的嵌套列表中,因此我有点卡住了。

lapply(Chars_alive, function(x) {addDetentions(x, rownames(x), Feb_detentions, Feb_detentions$Pupil)})

任何帮助将不胜感激。


一种选择是在list的嵌套 data.frame 和以与名称(第一个list的月份名称)相同的顺序创建的相应list之间进行mergeMap遍历相应的list元素

Map(function(x, y) 
# x is the first list which is a nested one
# so loop through it
lapply(x, function(dat) {
# create a Pupil column from the row names
dat$Pupil <- row.names(dat)
# merge with the corresponding 'detentions' dataset
merge(dat, y)
}),
# first list, created list
lst1, list(Feb_detentions, Mar_detentions)) 

使用tidyverse,这可以使用map2

library(tidyverse)
map2(lst1, list(Feb_detentions, Mar_detentions),
~ {
ydat <- .y
map(.x, ~ .x %>%
rownames_to_column("Pupil") %>% 
inner_join(ydat))
})

更新

如果我们只需要从"lst1"更新第二个嵌套list,只需提取该list元素并执行merge

Map(function(x, y)  x[[2]] <- {
x[[2]]$Pupil <- row.names(x[[2]])
merge(x[[2]], y)
x
}, lst1, list(Feb_detentions, Mar_detentions))

最新更新