R: combine several gsub() function in a pipe
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".
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.
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