如何通过字符串识别功能使用lapply将新列添加到嵌套列表中

时间:2019-04-05 16:41:41

标签: r list 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 $ Pupil),]-> 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

dput(head(输出)):

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)})

任何帮助在这里都将不胜感激。


1 个答案:

答案 0 :(得分:1)

一个选择是在merge的嵌套data.frames和与名称(第一个{ {1}})。 list遍历相应的list元素

list

使用Map,可以使用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)) 

更新

如果我们只需要更新'lst1'中的第二个嵌套tidyverse,只需提取该map2元素并执行library(tidyverse) map2(lst1, list(Feb_detentions, Mar_detentions), ~ { ydat <- .y map(.x, ~ .x %>% rownames_to_column("Pupil") %>% inner_join(ydat)) })

list