简体   繁体   English

在列表上使用 lapply 并添加带有数据框名称的列

[英]Using lapply over a list and adding a column with data frame name

I have a list containing two data frames:我有一个包含两个数据框的列表:

sample_list <- list("tables" = data.frame(weight = sample(1:50, 20, replace = T)),
                    "chairs" = data.frame(height = sample(1:50, 20, replace = T)))

I would like to use lapply to run a function over all the data frames in this list.我想使用lapply在此列表中的所有数据帧上运行 function。 In the output of each function, I need to create another column with the name of the source data frame (see mutate ):在每个 function 的 output 中,我需要使用源数据框的名称创建另一列(请参阅mutate ):

lapply(sample_list, function(x) {
  x %>% 
    filter(x >= 20) %>% 
    mutate(groupName = names(x))
})

For some reason, I can't figure out how to make this work.出于某种原因,我无法弄清楚如何使这项工作。 How do I pass the name of the data frame into mutate?如何将数据框的名称传递给 mutate? Right now it is returning the name of the first column in that data frame, rather than the name of the data frame itself.现在它正在返回该数据框中第一列的名称,而不是数据框本身的名称。

Thanks!谢谢!

We can loop through names of sample_list instead of looping through the list我们可以遍历sample_listnames而不是遍历列表

lapply(names(sample_list), function(x) {
    sample_list[[x]] %>% 
        filter_at(vars(1),~. >= 20) %>% 
        mutate(groupName = x)
})

Update Sep-2021 2021 年 9 月更新

cleaner way using purrr::map使用purrr::map更清洁方式

purrr::map(names(sample_list), ~sample_list[[.x]] %>% 
             filter_at(vars(1),~. >= 20) %>% 
             mutate(groupName = .x)
)

You can try purrr::imap() to map over both elements and elements' name.您可以在元素和元素名称上尝试purrr::imap()到 map。

# purrr::imap
purrr::imap(sample_list, function(element,name){
    head(mutate(element,groupName = name))
})

# or mapply, but you need to specify names of the list
myfun <- function(element,name){
    head(mutate(element,groupName = name))
}

mapply(myfun,sample_list,names(sample_list),SIMPLIFY = FALSE)

$tables
  weight groupName
1     42    tables
2     24    tables
3     13    tables
4     31    tables
5      9    tables
6     27    tables

$chairs
  height groupName
1     18    chairs
2      6    chairs
3     34    chairs
4     37    chairs
5     36    chairs
6     49    chairs

Using Map from base R使用来自base R Map Map

Map(function(dat, grp) cbind(dat, group_name = grp)[dat[[1]] > 20,], 
             sample_list, names(sample_list))

You can use Map with the function data.frame to add the names.您可以使用Map和 function data.frame来添加名称。

Map(`data.frame`, sample_list, groupName = names(sample_list))
#Map(`[<-`, sample_list, "groupName", value = names(sample_list)) #Alternative

#$tables
#   weight groupName
#1      22    tables
#2      12    tables
#3       9    tables
#4      26    tables
#5      39    tables
#6       6    tables
#7      31    tables
#8       9    tables
#9      39    tables
#10      4    tables
#11     37    tables
#12     30    tables
#13     20    tables
#14     35    tables
#15     31    tables
#16     46    tables
#17     44    tables
#18     30    tables
#19     12    tables
#20     46    tables
#
#$chairs
#   height groupName
#1      12    chairs
#2      17    chairs
#3      35    chairs
#4      40    chairs
#5      23    chairs
#6      21    chairs
#7      48    chairs
#8      24    chairs
#9      20    chairs
#10     41    chairs
#11     43    chairs
#12     45    chairs
#13     47    chairs
#14     13    chairs
#15     35    chairs
#16     32    chairs
#17     26    chairs
#18     34    chairs
#19     33    chairs
#20      8    chairs

In case it should also be subseted to those >= 20 :如果它也应该被划分为那些>= 20

lapply(sample_list, function(x) x[x[,1] >= 20,, drop = FALSE])

When it should be done in one step I would use the way already posted by @akrun.当它应该一步完成时,我会使用@akrun 已经发布的方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用数据框的名称(删除,替换和套用)重命名数据框列表中的第二列 - Rename the Second Column in a List of Data Frames using the Name of the Data Frame (deparse, substitute, and lapply) 使用lapply将均值函数应用于数据帧列表 - Using lapply to apply mean function over list of data frame 数据框列表 - 根据数据框名称添加新列 - Data frame list - Adding a new column based on data frame name 使用 lapply 对数据框列表进行分组 - Using lapply over a list of data frames with grouping 将stargazer与通过对拆分data.frame进行重叠处理而创建的lm对象列表一起使用 - Using stargazer with a list of lm objects created by lapply-ing over a split data.frame 在data.tables列表上使用lapply将列表成员名称分配为变量 - Using lapply over list of data.tables to assign list member name as variable 如何从使用lapply传递给函数的列表中获取data.frame的名称 - how to get name of data.frame from list passed to function using lapply R:使用lapply创建新列和值,并在data.frame列表中应用嵌套,输出错误 - R:create new column and value using lapply & apply nested on data.frame list, wrong output 传递给 lapply 时是否可以返回列表中数据框的名称? - Is it possible to return the name of a data frame in a list when passed to lapply? 使用lapply对数据帧的行进行精确的二项式测试 - Using lapply to run exact binomial test over rows of a data frame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM