R: how to subset a data.frame in a list and return data.frame?

后端 未结 1 1482
北荒
北荒 2021-01-13 09:12

When subsetting a data.frame inside of a list, I get vectors instead of a data.frames (see the example below). How to avoid this and get a data.frames?

l <         


        
相关标签:
1条回答
  • 2021-01-13 09:43

    You need the ,drop=FALSE argument

    > res <- lapply(l, function(x) x[2:nrow(x),, drop=FALSE])
    > sapply(res,class)
               A            B            C 
    "data.frame" "data.frame" "data.frame" 
    > res
    $A
      a
    2 2
    3 3
    
    $B
      b
    2 5
    3 6
    4 5
    
    $C
      c
    2 4
    3 5
    4 6
    
    0 讨论(0)
提交回复
热议问题