Perform mutate in columns simultaneously

Asked

Viewed 139 times

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?

1 answer

4


You can do it like this:

library(tidyverse)

df %>% 
  mutate_at(.vars = vars(contains('col')), .funs = funs(. = percentualize))

  col1 col2 col3 col1_. col2_. col3_.
1    1    4    7     1%     4%     7%
2    2    5    8     2%     5%     8%
3    3    6    9     3%     6%     9%

Editing

If variables have something in common, you could use this feature to call them all at once. Consider the new names below:

df %>%
  mutate(coluna = percentualize(col1),
         variavel = percentualize(col2),
         lambda = percentualize(col3))

As all variables have the letter l in common, you can do this:

df %>% 
  mutate_at(.vars = vars(contains('l')), .funs = list(. = percentualize))

That would give the same result:

  col1 col2 col3 col1_. col2_. col3_.
1    1    4    7     1%     4%     7%
2    2    5    8     2%     5%     8%
3    3    6    9     3%     6%     9%

Variables may not have common names, often. But their classes may have similarity. So you can do an analysis by the object class, not by its names. Suppose its variables are class numeric. With mutate_if you do that:

df %>% 
  mutate_if(is.numeric, .funs = funs(. = percentualize))

  col1 col2 col3 col1_. col2_. col3_.
1    1    4    7     1%     4%     7%
2    2    5    8     2%     5%     8%
3    3    6    9     3%     6%     9%

Anything of a class numeric will be considered in the analysis.

  • I understood, but if the variables had different names, for example "variable" and "column" I could do mutate_at(.vars = vars(variavel,coluna) , ...) ?

  • You could. For example, if they had a common pattern (common letter), you could use regex to identify the variables.

  • I will edit with an example.

  • 1

    It would have to be vars("variavel", "coluna").

  • Or that that @Rui proposes.

  • 1

    @Jessicavoigt Or vars(col1:col3) or with column numbers vars(1:3).

  • Thank you guys!

  • The mutate_if I deserved one more vote, but I can’t...

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.