How to read data when some numbers contain commas as thousand separator?

后端 未结 11 1429
情书的邮戳
情书的邮戳 2020-11-22 02:29

I have a csv file where some of the numerical values are expressed as strings with commas as thousand separator, e.g. \"1,513\" instead of 1513. Wh

11条回答
  •  余生分开走
    2020-11-22 02:42

    a dplyr solution using mutate_all and pipes

    say you have the following:

    > dft
    Source: local data frame [11 x 5]
    
       Bureau.Name Account.Code   X2014   X2015   X2016
    1       Senate          110 158,000 211,000 186,000
    2       Senate          115       0       0       0
    3       Senate          123  15,000  71,000  21,000
    4       Senate          126   6,000  14,000   8,000
    5       Senate          127 110,000 234,000 134,000
    6       Senate          128 120,000 159,000 134,000
    7       Senate          129       0       0       0
    8       Senate          130 368,000 465,000 441,000
    9       Senate          132       0       0       0
    10      Senate          140       0       0       0
    11      Senate          140       0       0       0
    

    and want to remove commas from the year variables X2014-X2016, and convert them to numeric. also, let's say X2014-X2016 are read in as factors (default)

    dft %>%
        mutate_all(funs(as.character(.)), X2014:X2016) %>%
        mutate_all(funs(gsub(",", "", .)), X2014:X2016) %>%
        mutate_all(funs(as.numeric(.)), X2014:X2016)
    

    mutate_all applies the function(s) inside funs to the specified columns

    I did it sequentially, one function at a time (if you use multiple functions inside funs then you create additional, unnecessary columns)

提交回复
热议问题