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

后端 未结 11 1538
情书的邮戳
情书的邮戳 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:46

    We can also use readr::parse_number, the columns must be characters though. If we want to apply it for multiple columns we can loop through columns using lapply

    df[2:3] <- lapply(df[2:3], readr::parse_number)
    df
    
    #  a        b        c
    #1 a    12234       12
    #2 b      123  1234123
    #3 c     1234     1234
    #4 d 13456234    15342
    #5 e    12312 12334512
    

    Or use mutate_at from dplyr to apply it to specific variables.

    library(dplyr)
    df %>% mutate_at(2:3, readr::parse_number)
    #Or
    df %>% mutate_at(vars(b:c), readr::parse_number)
    

    data

    df <- data.frame(a = letters[1:5], 
                     b = c("12,234", "123", "1,234", "13,456,234", "123,12"),
                     c = c("12", "1,234,123","1234", "15,342", "123,345,12"), 
                     stringsAsFactors = FALSE)
    

提交回复
热议问题