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

亡梦爱人 提交于 2019-12-30 09:43:35

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/6941985/r-how-to-subset-a-data-frame-in-a-list-and-return-data-frame

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!