Skip to content
Prev 361182 / 398506 Next

Computing means of multiple variables based on a condition

Another option would be to convert the data into a long format and add
columns for each condition.

library(dplyr)
library(tidyr)
DF %>%
  gather(key = "key", value = "value", -a, -d) %>%
  mutate(
    "d>=2" = ifelse(d >= 2, value, NA),
    "d>=4" = ifelse(d >= 4, value, NA),
    "d>=6" = ifelse(d >= 6, value, NA)
  ) %>%
  select(-d, -value) %>%
  gather(key = "condition", value = "value", -a, -key, na.rm = TRUE) %>%
  group_by(a, key, condition) %>%
  summarise(mean = mean(value)) %>%
  spread(key = key, value = mean) %>%
  arrange(condition, a)


ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and
Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey

2016-05-26 8:34 GMT+02:00 Jeff Newmiller <jdnewmil at dcn.davis.ca.us>: