R:dplyr没有认识到聚集的领域

时间:2016-03-15 19:08:31

标签: r dplyr

使用以下KoreaEx数据:重命名列并收集国家,年份和出口。再次重命名列以正确标记Month和Exports,然后尝试group_by并汇总但NA返回。是不能识别的数值,代码是不正确的?

> head(KoreaEx)
    CTYNAME year  EJAN  EFEB  EMAR  EAPR  EMAY  EJUN  EJUL  EAUG  ESEP  EOCT  ENOV  EDEC
9  Korea, South 2009 1,807 2,074 1,835 2,024 2,474 2,295 2,372 2,642 2,711 2,842 2,797 2,739
27 Korea, South 2010 2,977 3,073 3,350 3,157 3,291 3,230 3,372 3,146 3,161 3,309 3,384 3,370
45 Korea, South 2011 3,507 3,146 3,520 3,861 3,881 3,490 3,626 3,785 3,569 3,471 3,820 3,785
63 Korea, South 2012 3,472 4,103 3,700 3,666 3,494 3,589 3,516 3,132 3,585 3,512 3,195 3,318
81 Korea, South 2013 3,389 3,521 3,413 3,162 3,270 3,446 3,470 3,439 3,164 3,582 4,040 3,789
99 Korea, South 2014 3,712 3,617 3,830 3,811 3,683 3,767 3,670 3,801 3,644 3,604 3,680 3,654

colnames(KoreaEx) = c("Country", "Year", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"    
KoreaEx = KoreaEx %>% gather(Country, Year, Jan:Dec)
colnames(KoreaEx) = c("Country", "Year", "Month", "Exports")

> head(KoreaEx)
     Country   Year    Month Exports
1 Korea, South 2009     Jan 1,807
2 Korea, South 2010     Jan 2,977
3 Korea, South 2011     Jan 3,507
4 Korea, South 2012     Jan 3,472
5 Korea, South 2013     Jan 3,389
6 Korea, South 2014     Jan 3,712

KorExp %>% group_by(Year) %>% summarise(Exports = sum(Exports, na.rm = FALSE))
Exports

1      NA
> 2:#1

0 个答案:

没有答案