R: combine several gsub() function in a pipe

前端 未结 4 1700
野性不改
野性不改 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条回答
  •  梦毁少年i
    2021-02-05 21:57

    The problem is that the argument that is fed into the pipe needs to be the first in the list of arguments. But this is not the case for gsub(), as x is the third one. A (wordy) workaround could be:

    df$A %>% 
      gsub(pattern = "\\.", replacement="") %>%
      str_trim() %>%
      gsub(patter = ",", replacement = ".") %>%
      as.numeric
    

提交回复
热议问题