4
Hello,
I have a dataframe where I want to apply the same function in several columns at the same time. I tried to use the dplyr::mutate_at
but I don’t think I understand the logic of this operation.
First what I can do:
df <- data.frame(col1 = c(1:3), col2= c(4:6), col3 = c(7:9))
percentualize <- function(x){
x <- paste0(x, "%")
}
library(tidyverse)
df %>%
mutate(col1 = percentualize(col1),
col2 = percentualize(col2),
col3 = percentualize(col3))
How could I operate in these three columns at once without having to keep repeating?
I understood, but if the variables had different names, for example "variable" and "column" I could do
mutate_at(.vars = vars(variavel,coluna) , ...) ?
– Jessica Voigt
You could. For example, if they had a common pattern (common letter), you could use
regex
to identify the variables.– neves
I will edit with an example.
– neves
It would have to be
vars("variavel", "coluna")
.– Rui Barradas
Or that that @Rui proposes.
– neves
@Jessicavoigt Or
vars(col1:col3)
or with column numbersvars(1:3)
.– Rui Barradas
Thank you guys!
– Jessica Voigt
The
mutate_if
I deserved one more vote, but I can’t...– Rui Barradas