I have a data frame that has many columns and many rows
col_1 | col_2 | ... | col_n
---------------------------
val_1 | val_2 | ... | val_n
val_1 | val_2 | ... | val_n
. | . | . | .
. | . | . | .
. | . | . | .
val_1 | val_2 | ... | val_n
My wish is to remove all commas ,
from all values with tidyverse.
How can I achieve this?
Use gsub
to replace commas and across
to apply it for multiple columns.
library(dplyr)
df %>% mutate(across(.fns = ~gsub(',', '', ., fixed = TRUE)))
Or in base R -
df[] <- lapply(df, function(x) gsub(',', '', x, fixed = TRUE))
We could use str_remove_all
and this should comply with tidyverse
library(dplyr)
library(stringr)
df <- df %>%
mutate(across(everything(), str_remove_all, ","))
A base R option using gsub
list2DF(lapply(df, function(v) gsub(",", "", v)))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.