0

This question is very similar to this one: Min and Median of Multiple Columns of a DF by Row in R And I know has been asked in a variety of ways, but I don't feel that they adequately address my question.

I have a data frame with 260ish columns. I'd like to get the median of every row, preferably using dplyr as its what I'm comfortable with. The answer from the above linked question is almost perfect, doing something like:

df %>% 
  rowwise() %>% 
   mutate(median = median(c(V1, V2, V3)))

Would work and accomplish exactly what I ask, but much like this question: Sum across multiple columns with dplyr

I'd rather not write out the names of 260ish columns. Is there a way to take the above command and modify it so it inputs all 260 columns?

Joe Crozier
  • 379
  • 1
  • 9
  • 3
    Look into `c_across`, e.g. `mutate(median = median(c_across())` – arg0naut91 Aug 31 '20 at 15:34
  • 1
    Worked perfectly once I realized my columns were all character class. Which is weird because if I just simply used median on two individual columns prior to fixing that, it worked just fine. Oh well. Thank you! – Joe Crozier Aug 31 '20 at 15:58
  • If all of your data is numeric, why can't you just use `apply` as suggested in your reference link? I.e., `medians – SteveM Aug 31 '20 at 20:24

1 Answers1

0

Here is an example using a numeric dataset and tidy verse

libraries used dplyr, purrr

library(tidyverse)


diabetes<-read.csv(url('https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.data.csv'))


diabetes$row_median = t(map(data.frame(t(diabetes)),median) %>% bind_cols())