l_ply:如何将列表的name属性传递给函数? [英] l_ply: how to pass the list's name attribute into the function?

查看:109
本文介绍了l_ply:如何将列表的name属性传递给函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个这样的R列表:

Say I have an R list like this:

> 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一起传递到处理函数中.像这样:

I want to process each data.frame in the list using l_ply, but I also need the name (e.g. aug9104AP) to be passed into the processing function along with the data.frame. Something like:

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)),例如:

In case you pass them one by one, you can use deparse(substitute(arg)) , eg :

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 l_ply, you'll have to resort to add the attribute to the list itself eg :

for(i in 1:length(data.list)){
    attr(data.list[[i]],"name") <- names(data.list)[i]
}

然后您可以使用attr:

Then you can use attr :

cli <- attr(x,"name")

欢呼

这篇关于l_ply:如何将列表的name属性传递给函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆