Mevve
Mevve

Reputation: 159

String substitutions across multiple columns in tidyverse

My data frame

>df <- data.frame(Names = c("A", "B", "C"), Total = c("125", "2 500", "1 350"), 
>           Boys = c("50", "1 500", "350"),
>           Girls = c("75", "1 000", "1 000")) 
  Names Total  Boys Girls
1     A   125    50    75
2     B 2 500 1 500 1 000
3     C 1 350   350 1 000

All values are strings. I want to substitute spaces " " to non spaces "" in Total, Boys and Girls. I know about

df %>%
  mutate(Total = gsub(" ", "", Total),
         Boys = gsub(" ", "", Boys),
         Girls = gsub(" ", "", Girls))

But is there a (tidyverse style) way to do this more generally? Something like

df %>% # (This don't work)
  mutate(across(c(Total, Boys, Girls), gsub(" ", "", .x)))

I.e. I'm looking for a solution that scales well.

Thanks in advance.

Upvotes: 2

Views: 706

Answers (3)

ThomasIsCoding
ThomasIsCoding

Reputation: 101099

Maybe you can try gsub + as.matrix like below

type.convert(cbind(df[1], gsub("\\s+", "", as.matrix(df[-1]))), as.is = TRUE)

which gives

  Names Total Boys Girls
1     A   125   50    75
2     B  2500 1500  1000
3     C  1350  350  1000

Upvotes: 0

akrun
akrun

Reputation: 886948

We can use str_remove from stringr

library(stringr)
library(dplyr)
df %>% 
    mutate(across(-Names, ~ as.numeric(str_remove(., "\\s+"))))
#  Names Total Boys Girls
#1     A   125   50    75
#2     B  2500 1500  1000
#3     C  1350  350  1000

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 388817

You can use :

library(dplyr)

df %>% mutate(across(c(Total, Boys, Girls), ~as.numeric(gsub(" ", "", .))))

#  Names Total Boys Girls
#1     A   125   50    75
#2     B  2500 1500  1000
#3     C  1350  350  1000

Or in base R with lapply :

df[-1] <- lapply(df[-1], function(x) as.numeric(gsub(" ", "", x)))

Upvotes: 2

Related Questions