list - l_ply : how to pass the list's name attribute into the function?

标签 list r plyr

假设我有一个这样的 R 列表:

> summary(data.list)
                                 Length Class      Mode
aug9104AP                        18     data.frame list
Aug17-10_acon_7pt_dil_series_01  18     data.frame list
Aug17-10_Picro_7pt_dil_series_01 18     data.frame list
Aug17-10_PTZ_7pt_dil_series_01   18     data.frame list
Aug17-10_Verat_7pt_dil_series_01 18     data.frame list

我想使用 l_ply 处理列表中的每个 data.frame ,但我还需要将名称(例如 aug9104AP)与 data.frame 一起传递到处理函数中。类似的东西:
l_ply(data.list,function(df,...) {

    cli.name<- arg_to_access_current_list_item_name

    #make plots with df, use cli.name in plot titles
    #save results in a file called cli.name

  }, arg_to_access_current_list_item_name
)

应该怎么做arg_to_access_current_list_item_name是?

最佳答案

如果您一一传递它们,您可以使用 deparse(substitute(arg)) ,例如:

test <- function(x){
       y <- deparse(substitute(x))
       print(y)
       print(x)
 }

 var <- c("one","two","three")
 test(var)
[1] "var"
[1] "one"   "two"   "three"

对于 l_ply,您必须求助于将属性添加到列表本身,例如:
for(i in 1:length(data.list)){
    attr(data.list[[i]],"name") <- names(data.list)[i]
}

然后你可以使用 attr :
cli <- attr(x,"name")

干杯

关于list - l_ply : how to pass the list's name attribute into the function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3548263/

相关文章:

r - 如何使用 mapply 复制嵌套 for 循环?

python - 根据来自另一个列表的 True/False 过滤列表中的元素

Python:如果包含/不包含顺序相同,则从列表中删除子列表

python - 如何在 python 中获取列表的子集?

r - 显示文件或对象之间的差异

r - 计算 R 中的迭代以计算人口增长率

r - 从 r 中的列表中创建指示变量

r - 按组汇总 wtd.quantile

r - data.frames 列表到 xts 对象列表

r - 使用数据表对子集执行操作