All, This may not be the proper place to ask as the question is dplyr and removing white space using mutate. ?My code is below: ```{r, echo = FALSE, message = FALSE conn <- myconnection ```` ```{r, echo = FALSE, message=FALSE} fhlloanbalance <- tbl(conn, "fhlloan_balance_view") colnames(fhlloanbalance) ``` ```{r, echo=FALSE, message=FALSE} fhlloanbalance %>% select(fctrdt, servicer) %>% filter(fctrdt == '2020-01-01') %>% mutate(servicer = 'I need to remove all white space') ``` I have tried a number of approaches using stringr - stringi - and gsub. ?The problem I think I am facing: I cannot seem to operate on the column in sql this is relative straight forward using replace. ?I have tried several approaches and scanned the internet but I cannot seem to come up with a workable solution. ?Any help is appreciated. ?I suppose I could just default back to sql but would prefer to use dplyr. Best, Glenn
I don't use dplyr, but it's trivial with gsub, assuming I understand correctly:> x <- "a b\t c\n e" > cat(x)a b c e> gsub("[[:space:]]", "",x)[1] "abce" See ?regex for details (of course!), especially the section on character classes. Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sun, Mar 1, 2020 at 6:33 PM Glenn Schultz via R-help < r-help at r-project.org> wrote:> All, > > This may not be the proper place to ask as the question is dplyr and > removing white space using mutate. My code is below: > > ```{r, echo = FALSE, message = FALSE > conn <- myconnection > ```` > > ```{r, echo = FALSE, message=FALSE} > fhlloanbalance <- tbl(conn, "fhlloan_balance_view") > colnames(fhlloanbalance) > ``` > > ```{r, echo=FALSE, message=FALSE} > fhlloanbalance %>% > select(fctrdt, servicer) %>% > filter(fctrdt == '2020-01-01') %>% > mutate(servicer = 'I need to remove all white space') > ``` > I have tried a number of approaches using stringr - stringi - and gsub. > The problem I think I am facing: I cannot seem to operate on the column in > sql this is relative straight forward using replace. I have tried several > approaches and scanned the internet but I cannot seem to come up with a > workable solution. Any help is appreciated. I suppose I could just > default back to sql but would prefer to use dplyr. > > Best, > Glenn > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Hello, Here are 2 examples, one with stringr::str_remove_all and the other with base gsub. I am assuming that you want to remove the spaces from column 'servicer'. dat <- data.frame(x = rnorm(2), servicer = c("a b c", " d efg"), fctrdt = 1:2) dat library(dplyr) library(stringr) dat %>% select(fctrdt, servicer) %>% mutate(servicer = str_remove_all(servicer, "[[:space:]]")) dat %>% select(fctrdt, servicer) %>% mutate(servicer = gsub("[[:space:]]", "", servicer)) Hope this helps, Rui Barradas ?s 14:42 de 01/03/20, Glenn Schultz via R-help escreveu:> All, > > This may not be the proper place to ask as the question is dplyr and > removing white space using mutate. ?My code is below: > > ```{r, echo = FALSE, message = FALSE > conn <- myconnection > ```` > > ```{r, echo = FALSE, message=FALSE} > fhlloanbalance <- tbl(conn, "fhlloan_balance_view") > colnames(fhlloanbalance) > ``` > > ```{r, echo=FALSE, message=FALSE} > fhlloanbalance %>% > select(fctrdt, servicer) %>% > filter(fctrdt == '2020-01-01') %>% > mutate(servicer = 'I need to remove all white space') > ``` > I have tried a number of approaches using stringr - stringi - and gsub. > ?The problem I think I am facing: I cannot seem to operate on the > column in sql this is relative straight forward using replace. ?I have > tried several approaches and scanned the internet but I cannot seem to > come up with a workable solution. ?Any help is appreciated. ?I suppose I > could just default back to sql but would prefer to use dplyr. > > Best, > Glenn > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.