-2

Is it possible to refer to 'all columns' or a range of columns in combination with rowwise() in dplyr without having to enumerate all individual column names?

For example, in the following, I would rather not specify each of the first 7 column names, but rather say 1:7 or something. Can't seem to get it to work. Must be missing something simple.

mtcars %>%
  rowwise() %>%
  mutate(sillyMetric = mean(c(mpg, cyl, disp, hp, drat, wt, qsec))) %>%
  .$sillyMetric

Please don't tell me how I don't need rowwise() for this case. I obviously have a different problem I am trying to solve that requires it. I want to know if there is a way to not have to enumerate all column names with rowwise. I DON'T want any other solutions that compute this above sillyMetric more efficiently.

Gopala
  • 10,363
  • 7
  • 45
  • 77
  • 2
    What about `rowMeans(mtcars[,1:7])`? – Jaap Jan 26 '18 at 15:30
  • 1
    :) Read my last comment. – Gopala Jan 26 '18 at 15:36
  • 7
    If you have a different problem you are trying to solve, then you should update your question to include an example that shows what kind of problem you are trying to solve. – Jaap Jan 26 '18 at 15:41
  • 2
    It is a minimal, reproducible example. Not sure why you are insisting I elaborate further as to what I need. I simply need to not have to enumerate the column names with `rowwise`. – Gopala Jan 26 '18 at 15:43
  • To silence the objectors, just use a slightly more complicated function that doesn't have a row-focused implementation. Replace `mean(c(...))` with `quantile(c(...), probs = 0.314)` – Gregor Thomas Jan 26 '18 at 15:57
  • 2
    See https://stackoverflow.com/questions/28751023/performing-dplyr-mutate-on-subset-of-columns. All the answers basically hack `rowMeans` (which would just be `mean` in your case) inside a `mutate` anyway. The simplicity normally gained by using `dplyr` over base seems outweighed by how hacky the solutions will end up. – twedl Jan 26 '18 at 16:47

1 Answers1

2

There is no way to do this with rowwise up front. You will probably need to take a step back and re-think how you are addressing the rest of your code. Being able to calculate based on the row ID may yield fruitful. Something like this:

mtcars %>%
  mutate(id = 1:n()) %>%
  gather(metric, val, 1:7) %>% 
  group_by(id) %>%
  summarise(SillyMetric = mean(val))

At worst you could just do this separately in then join it in with a left join.

Zafar
  • 1,897
  • 15
  • 33