R dplyr的过滤器功能不适用于重命名的因子变量

时间:2015-08-19 23:26:08

标签: r dplyr

我有一个data.frame,结构如下:

Classes ‘grouped_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 16 obs. of  3 variables:
 $ type  : Factor w/ 4 levels "NON-ROAD","NONPOINT",..: 1 1 1 1 2 2 2 2 3 3 ...
 $ year  : Ord.factor w/ 4 levels "1999"<"2002"<..: 1 2 3 4 1 2 3 4 1 2 ...
 $ sum.pm: num  522.9 240.8 248.9 55.8 2107.6 ...
 - attr(*, "vars")=List of 1
  ..$ : symbol type
 - attr(*, "drop")= logi TRUE

通过这种结构,我可以根据年变量选择行,例如

reduced = filter(pm.baltimore.type, year == 1999)

但是,如果我更改变量名称,即

new.var.names <- colnames(pm.baltimore.type)
new.var.names[1] <- "EmissionSource"
new.var.names[2] <- "Year"
new.var.names[3] <- "PM2.5"
colnames(pm.baltimore.type) <- new.var.names

给我这个结构:

Classes ‘grouped_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 16 obs. of  3 variables:
 $ EmissionSource: Factor w/ 4 levels "NON-ROAD","NONPOINT",..: 1 1 1 1 2 2 2 2 3 3 ...
 $ Year          : Ord.factor w/ 4 levels "1999"<"2002"<..: 1 2 3 4 1 2 3 4 1 2 ...
 $ PM2.5         : num  522.9 240.8 248.9 55.8 2107.6 ...
 - attr(*, "vars")=List of 1
  ..$ : symbol type
 - attr(*, "drop")= logi TRUE

这失败了:

reduced = filter(pm.baltimore.type, Year == 1999)

我收到此错误:错误:未知列'type'

0 个答案:

没有答案