tapply() 输出错误 return 因子向量

Error in tapply() output return a vector of factors

当使用 read.csv() 将数据帧读入 R 并使用 tapply() 计算治疗方法时,结果是一个因子向量。

cfpr<-read.csv("E:/temp/vars.csv",sep=";")
 cfpr
    ano mes usd_brl      x        y
1  2014   5   2.221 181.83 403.8444
2  2014   6   2.236 172.37 385.4193
3  2014   7   2.225 169.27 376.6257
4  2014   8   2.268 175.89 398.9185
5  2015   5   3.064 144.79 443.6366
6  2015   6   3.111 151.12 470.1343
7  2015   7   3.224 135.75 437.6580
8  2015   8   3.515 135.27 475.4740
9  2016   5   3.549 135.26 480.0377
10 2016   6   3.418 145.22 496.3620
11 2016   7   3.278 155.80 510.7124
12 2016   8   3.208 156.61 502.4049
 class(cfpr$ano)
[1] "integer"
 class(cfpr$y)
[1] "numeric"
 tapply(cfpr$y,cfpr$ano,fun=mean)
 [1] 1 1 1 1 2 2 2 2 3 3 3 3

如果数据框列重命名 tapply() 再次工作:

> cfpr<-read.csv("E:/temp/vars.csv",sep=";")
> cfpr
    ano mes usd_brl      x        y
1  2014   5   2.221 181.83 403.8444
2  2014   6   2.236 172.37 385.4193
3  2014   7   2.225 169.27 376.6257
4  2014   8   2.268 175.89 398.9185
5  2015   5   3.064 144.79 443.6366
6  2015   6   3.111 151.12 470.1343
7  2015   7   3.224 135.75 437.6580
8  2015   8   3.515 135.27 475.4740
9  2016   5   3.549 135.26 480.0377
10 2016   6   3.418 145.22 496.3620
11 2016   7   3.278 155.80 510.7124
12 2016   8   3.208 156.61 502.4049
> colnames(cfpr)[4:5]<-c("X","Y")
> class(cfpr$ano)
[1] "integer"
> class(cfpr$Y)
[1] "numeric"
> tapply(cfpr$Y,cfpr$ano,mean)
    2014     2015     2016 
391.2020 456.7257 497.3792 

如何避免此错误并每次都重命名数据?

Link 到我正在使用的数据:https://drive.google.com/open?id=1PhiuQIptVNylPFohDpl5AIDr94xxlf5p

附加信息:

> sessionInfo()
R version 3.4.2 (2017-09-28)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 (build 7601) Service Pack 1

Matrix products: default

locale:
[1] LC_COLLATE=Portuguese_Brazil.1252  LC_CTYPE=Portuguese_Brazil.1252    LC_MONETARY=Portuguese_Brazil.1252
[4] LC_NUMERIC=C                       LC_TIME=Portuguese_Brazil.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_3.4.2 tools_3.4.2  

这里的问题不在于列名,而在于参数,您正在传递函数 tapply。

下面的代码片段解释了您的疑问。

> cfpr = read.csv("vars.csv",sep = ';')
> head(cfpr)
   ano mes usd_brl      x        y
1 2014   5   2.221 181.83 403.8444
2 2014   6   2.236 172.37 385.4193
3 2014   7   2.225 169.27 376.6257
4 2014   8   2.268 175.89 398.9185
5 2015   5   3.064 144.79 443.6366
6 2015   6   3.111 151.12 470.1343
> class(cfpr$ano)
[1] "integer"
> class(cfpr$y)
[1] "numeric"
> ## Method 1
> tapply(cfpr$y, cfpr$ano, mean)
    2014     2015     2016 
391.2020 456.7257 497.3792 
> ## Method 2
> tapply(cfpr$y, cfpr$ano, FUN = function(x){mean(x)})
    2014     2015     2016 
391.2020 456.7257 497.3792 

注意,如果需要传递函数来申请,需要给参数FUN。如需更多参考资料,请输入 ?tapply.

查看 tapply 的文档

希望这能消除您的疑虑。