Print the Nth Row in a List of Data Frames

后端 未结 4 1403
一向
一向 2020-12-30 14:25

I am cleaning several excel files in R. They unfortunately are of unequal dimensions, rows and columns. Currently I am storing each excel sheet as a data frame in a list. I

4条回答
  •  醉梦人生
    2020-12-30 14:56

    There is no need for a wrapper function, just use lapply and pass it a blank argument at the end (to represent the columns)

    lapply(df.list, `[`, 4, )
    

    This also works with any type of row argument that you would normally use in myDF[ . , ] eg: lapply(df.list,[, c(2, 4:6), )

    .


    I would suggest that if you are going to use a wrapper function, have it work more like [ does: eg

    Grab(df.list, 2:3, 1:5) would select the second & third row and first through 5th column of every data.frame and Grab (df.list, 2:3) would select the second & third row of all columns

    Grab <- function(ll, rows, cols) {
        if (missing(cols))
            lapply(ll, `[`, rows, )
        else 
            lapply(ll, `[`, rows, cols)
    }
    
    Grab (df.list, 2:3)
    

提交回复
热议问题