R:按列数划分的数据框子集列表?
R: Subset list of data frames by number of columns?
我在按简单条件(列数)对包含数据框的列表进行子集化时遇到问题。
这是我的数据集:
d1<-data.frame(a=rnorm(5), b=c(rep(2006, times=4),NA), cc=c(1:5))
d2<-data.frame(a=1:5, b=c(2007, 2007, NA, NA, 2007))
我的名单:
ls1<-list(d1, d2)
如何从 ls1
中精确包含 3 列的数据帧进行子集化?
我试过了
lapply(ls1, subset, ncol = 3)
但它不起作用..谢谢!!
ls1[sapply(ls1,ncol) == 3]
或
Filter(function(x) ncol(x) == 3, ls1)
我在按简单条件(列数)对包含数据框的列表进行子集化时遇到问题。
这是我的数据集:
d1<-data.frame(a=rnorm(5), b=c(rep(2006, times=4),NA), cc=c(1:5))
d2<-data.frame(a=1:5, b=c(2007, 2007, NA, NA, 2007))
我的名单:
ls1<-list(d1, d2)
如何从 ls1
中精确包含 3 列的数据帧进行子集化?
我试过了
lapply(ls1, subset, ncol = 3)
但它不起作用..谢谢!!
ls1[sapply(ls1,ncol) == 3]
或
Filter(function(x) ncol(x) == 3, ls1)