0

With KoreaEx data below: renamed columns and gathered Country, Year and Exports. Renamed columns once again to correctly label Month and Exports, then tried to group_by and summarise but NA returns. is the numeric values not being recognized, is the code incorrect?

> 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
danielhong
  • 77
  • 2
  • 9
  • 4
    Hint: 1,807 != 1807. – Hong Ooi Mar 15 '16 at 19:10
  • Nice point @ Hong Ooi I've had this issue before (didn't notice that there was commas until Hong pointed it out), and couldn't figure out how to remove commas in R, ended up doing it in Excel. Is there a way to remove them in R or work around this? – InfiniteFlash Mar 15 '16 at 19:18
  • 3
    Of course. `as.numeric(sub(",", "", c("1,100", "1,200")))`. – Roman Luštrik Mar 15 '16 at 19:20
  • Even after applying 'KoreaEx$Exports <- as.numeric(gsub(",","",KoreaEx$Exports))' It still returns NA, is there something else that could be wrong with my code? – danielhong Mar 15 '16 at 19:42

0 Answers0