Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Indexing list of data.frames – how to get all x-th columns?

Tags:

indexing

r

I have list of data.frames and I wonder whether there is an easy indexing way of getting all third columns of all data.frames. Or all columns named x? Speaking R:

lapply(names(mylist),function(x) mylist[[x]][,3])

Is there any way to do it by just indexing, like mylist[[]][,3]? (which does not work obviously)

EDIT: And how do you do that, when you want to use a function like nlevels in that, like

  lapply(names(mylist),function(x) nlevels(mylist[[x]][,3]))

given that column 3 is a factor.

like image 547
Matt Bannert Avatar asked Feb 03 '23 08:02

Matt Bannert


1 Answers

Maybe this is somewhat easier:

lapply(mylist, "[[", 3)
lapply(mylist, "[[", name_of_column)
like image 195
kohske Avatar answered Feb 05 '23 01:02

kohske