Home > Blockchain >  How can I index a list of data frames by rowname?
How can I index a list of data frames by rowname?

Time:02-03

I have a list of lists of different length dataframes (list containing lists containing data frames) that contain a row called "net_score" that I want to access. below is an example of a list of the data frames

alist <- list(a = as.data.frame(c(1,2,3,7,5,0)),b = as.data.frame(c(4,5,2,5,7,4)))
alist[[1]]
        c(1, 2, 3, 4, 5,6)
1                        1
2                        2
3                        3
net_score                7
5                        5
6                        0

i want to call a row by name (say 4) and get the value (7). I want to access the net_score in each data frame. I prefer to call it by name but could call it by location (it is always 3 from the bottom/end). I want to sum them up. I have tried using lapply

lapply(alist,'[',["net_score",])   #this fails
lapply (alist_of_lists,'[',1) #this works to access my first list in list
#I want
net_score    7
net_score    5

but I cannot use it to simply print/output the value I want.

edit: added clarity and example of format

CodePudding user response:

Try lapply(list, function(x) x["net_score", ])

  •  Tags:  
  • Related