R: combine several gsub() function in a pipe

前端 未结 4 1699
野性不改
野性不改 2021-02-05 21:21

To clean some messy data I would like to start using pipes %>%, but I fail to get the R code working if gsub() is not at the beginning of the pipe,

4条回答
  •  滥情空心
    2021-02-05 22:09

    Normally one applies the pipes to the data frame as a whole like this returning the cleaned data frame. The idea of functional programming is that objects are immutable and are not changed in place but rather new objects are generated.

    library(dplyr)
    
    df %>%
       mutate(C = gsub("\\.", "", A)) %>%
       mutate(C = gsub(",", ".", C)) %>%
       mutate(C = as.numeric(C))
    

    Also note that these alternatives work:

    df %>% mutate(C = gsub("\\.", "", A), C = gsub(",", ".", C), C = as.numeric(C))
    
    
    df %>% mutate(C = read.table(text = gsub("[.]", "", A), dec = ",")[[1]])
    
    
    df %>% mutate(C = type.convert(gsub("[.]", "", A), dec = ","))
    

    For this particular example type.convert seems the most appropriate since it compactly expresses at a high level what we intend to do. In comparison, the gsub/as.numeric solutions seem too low level and verbose while read.table adds conversion to data.frame which we need to undo making it too high level.

提交回复
热议问题