Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dplyr use select() helpers inside mutate() [duplicate]

Tags:

r

dplyr

I'd to make a new variable which represents the sum (or other function) of many other variables which all start with "prefix_". Is there a way to do this neatly using these select() helpers (e.g. starts_with())?

I don't think mutate_at() works for this since I'm just trying to create a single new variable based on many existing variables.

My attempt:

df %<>%
  mutate(newvar = sum(vars(starts_with("prefix_"))))

This of course doesn't work. Many thanks!

A reproducible example:

mtcars %<>% 
  rename("prefix_mpg" = mpg) %>% 
  rename("prefix_cyl" = cyl) %>% 
  mutate(newvar = sum(var(starts_with("prefix_"))))

Intended output would be mtcars$newvar which is the sum of prefix_mpg and prefix_cyl. Of course I could just explicitly name mpg and cyl but in my actual case it's a long list of variables, too long to name conveniently.

like image 320
lost Avatar asked Oct 19 '25 07:10

lost


1 Answers

We can use starts_with with the select call and put them in the rowSums function. . is a way to specify the object from the output of the previous pipe operation.

library(dplyr)

mtcars %>% 
  rename(prefix_mpg = mpg, prefix_cyl = cyl) %>% 
  mutate(newvar = rowSums(select(., starts_with("prefix_"))))
like image 193
www Avatar answered Oct 20 '25 21:10

www