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
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(da开发者_开发百科ta.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
)
What should arg_to_access_current_list_item_name be?
It's easiest to start with the names, and then use them to extract the bit you're interested in:
l_ply(names(data.list),function(name,...) {
  df <- data.list[[name]]
)
You can also use m_ply to pass in both the name and data:
m_ply(cbind(names(data.list), data.list), function(name, df, ...) {
   ...
}
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"
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]
}
Then you can use attr :
cli <- attr(x,"name")
Cheers
Joris answer is the cleanest way to do this. I would add a function to extract attribute:
for(ename in names(data.list)) {
    attr(data.list[[ename]], "ename") <- ename
}
ename <- function(x) attr(x, "ename") # states for element name
So you use it as:
l_ply(data.list, function(df, ...) {
    cli.name<- ename(df)
    # make plots, save results using cli.name
})
I usually use this method:
l_ply(names(data.list), function(cli.name, df=data.list[[cli.name]], ...) {
    # make plots, save results using cli.name
})
Iterate over names and extract data.frame from original list using them.
Just for the notice there is a hack. I don't recommend it cause it mess with frames and it's hard to control.
Using fact that llply is actually a for loop you can extract actual step from the inside of a function. It can be done using get with the correct environment.
l_ply(data.list, function(df, ...) {
    cli.name<- names(data.list)[get("i",parent.frame())]
    # make plots, save results using cli.name
})
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论