Access and preserve list names in lapply function

前端 未结 6 1578
遇见更好的自我
遇见更好的自我 2020-12-01 01:33

I need to access list names inside the lapply function. I\'ve found some threads online where it\'s said I should iterate through the names of the list to be able to fetch e

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-01 01:56

    Have you looked into llply() from the package plyr?

    It does exactly what you are asking for. For each element of a list, apply function, keeping results as a list. llply is equivalent to lapply except that it will preserve labels and can display a progress bar. from ?llply

    mylist <- list(foo1=1:10,foo2=11:20)
    >names(mylist)
    [1] "foo1" "foo2"
    newlist<- llply(mylist, function(x) mean(x))
    
    >names(newlist)
    [1] "foo1" "foo2"
    

提交回复
热议问题