Reputation: 1222
I have a list of data frames which I need to obtain the last row of the 2nd column from. All the data frames have differing number of rows. I've already written code using lapply which can extract any row by variable "num" (returning NA for numbers which exceed the row length of the data frames) , however I want to include a variable num="worst" which will return the last row, 2nd column of available data. This is the code to retrive the "nth" row (xyz is the list of data frames):
if(num=="best"){num=as.integer(1)} else
(num=as.integer())
rownumber<-lapply(xyz, "[", num, 2, drop=FALSE)
Been cracking my head all day trying to find a solution to declare num=="worst". I want to avoid loops hence my use of lapply, but perhaps there is no other way?
Upvotes: 7
Views: 12064
Reputation: 8425
My understanding of the question is that you want a function that returns the second column of a data.frame
from a list
of dataframes, with an optional argument worst
that allows you to restrict it to the last observation.
I think the siimplest way to do this is to write a helper function, and then apply it to your list using lapply
.
I have written a selector
function that takes a row and column argument, as well as a worst
argument. I think this does everything you need.
df1 <- data.frame(A = rnorm(10), B = rnorm(10), C = rnorm(10))
df2 <- data.frame(A = rnorm(10), B = rnorm(10), C = rnorm(10))
ldf <- list(df1, df2)
selector <- function(DF, col, row=NULL, worst=FALSE){
if(!is.null(row)) return(DF[row, col])
if(!missing("col")) if(col > ncol(DF)) return(NA)
if(!is.null(row)) if(row > nrow(DF)) return(NA)
if(worst) {
tail(DF[,col, drop=F],1)
} else {
DF[row, col, drop=FALSE]
}
}
lapply(ldf, selector, worst=T)
Upvotes: 2