Use object names within a list in lapply/ldply

前端 未结 4 1402
攒了一身酷
攒了一身酷 2021-01-12 10:35

In attempting to answer a question earlier, I ran into a problem that seemed like it should be simple, but I couldn\'t figure out.

If I have a list of dataframes:

4条回答
  •  春和景丽
    2021-01-12 11:05

    In your definition, df.list does not have names, however, even then the deparse substitute idiom does not appear to work easilty (as lapply calls .Internal(lapply(X, FUN)) -- you would have to look at the source to see if the object name was available and how to get it

    Something like

    names(df.list) <- paste('df', 1:3, sep = '')
    
    foo <- function(n, .list){
             .list[[n]]$id <- n
             .list[[n]]
           } 
    
         a          x id
    1 1  0.8204213  a
    2 2 -0.8881671  a
    3 3  1.2880816  a
    4 1 -2.2766111  b
    5 2  0.3912521  b
    6 3 -1.3963381  b
    7 1 -1.8057246  c
    8 2  0.5862760  c
    9 3  0.5605867  c
    

提交回复
热议问题