Search code examples
rdataframesubset

How to subset a data.frame in a list and return data.frame?


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 <- list(data.frame(a=c(1,2,3)), data.frame(b=c(4,5,6,5)), data.frame(c=c(3,4,5,6)))
names(l) <- c("A", "B", "C")
l
lapply(l, function(x) x[2:nrow(x), ])

output

> l <- list(data.frame(a=c(1,2,3)), data.frame(b=c(4,5,6,5)), data.frame(c=c(3,4,5,6)))
> names(l) <- c("A", "B", "C")
> l
$A
  a
1 1
2 2
3 3

$B
  b
1 4
2 5
3 6
4 5

$C
  c
1 3
2 4
3 5
4 6

> lapply(l, function(x) x[2:nrow(x), ])
$A
[1] 2 3

$B
[1] 5 6 5

$C
[1] 4 5 6

Solution

  • 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