R: combine several gsub() function in a pipe

前端 未结 4 1697
野性不改
野性不改 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:01

    Try this:

    library(stringr)
    
    df$D <- df$A %>%
      { gsub("\\.","", .) } %>%
      str_trim() %>%
      { as.numeric(gsub(",", ".", .)) }
    

    With pipe your data are passed as a first argument to the next function, so if you want to use it somewhere else you need to wrap the next line in {} and use . as a data "marker".

提交回复
热议问题