Print the last row from a list of data frames

11,650

Solution 1

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)

Solution 2

How about...

lapply(xyz, tail, 1)
Share:
11,650
clattenburg cake
Author by

clattenburg cake

Updated on June 05, 2022

Comments

  • clattenburg cake
    clattenburg cake almost 2 years

    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?

  • ricardo
    ricardo over 10 years
    @Jiber, i don't think this answers the actual question -- the OP wanted help writing a function that selects row and column combinations, with an argument worst that returns the last row.