R循环数据帧以创建新的数据帧

时间:2014-09-06 06:40:42

标签: r loops dataframe lapply

假设我有几个数据框

dfx01=data.frame(city=c("a","b","c","d"),yr=c(2000,2001,2003,2002))
dfx02=data.frame(city=c("a","e","c","d"),yr=c(2000,2001,2005,2002))
dfx012=data.frame(city=c("f","b","c","d"),yr=c(2000,2000,2001,2002))
dfx022=data.frame(city=c("f","b","c","g"),yr=c(2002,2000,2003,2001))

我应该如何输出相应的数据帧x01,x02,x012,x022只能设置yr = 2001?

我尝试了lapply

dflist=list(dfx01,dfx02,dfx012,dfx022)
lapply(dflist, fun(x){subset(x,startyr=2000)})

但如何命名4个新数据帧x01,x02,x012,x022?感谢。

1 个答案:

答案 0 :(得分:0)

您的通话需要稍微更改一下。尝试

lapply(dflist, subset, yr == 2000)

但我更喜欢[子集,因为subset会产生意想不到的结果。这是如何做到这一点,并同时添加新名称。要设置与数据框名称类似的名称,最好先将名称添加到列表中。

> dflist <- setNames(dflist, grep("dfx0", ls(), value = TRUE))
> setNames(lapply(dflist, function(x) x[x$yr==2001, ]), 
                  gsub("df", "", names(dflist)))
# $x01
#   city   yr
# 2    b 2001
# 
# $x012
#   city   yr
# 2    e 2001
# 
# $x02
#   city   yr
# 3    c 2001
# 
# $x022
#   city   yr
# 4    g 2001