1
votes

With a data frame like below, need to aggregate (mean) across columns for each row.

> df <- data.frame(
  id = c(1,2), 
  loc=c("loc1", "loc2"), 
  val1=c(10,20), val2=c(190,NA), val3=c(NA, 10)
)
> df
  id  loc val1  val2  val3
1  1 loc1   10   190    NA
2  2 loc2   20   NA     10

With regular aggregation by column could have done

df %>%
 group_by(id, loc) %>%
 summarise(mean_val1 = mean(val1))

How do I compute the mean of val1, val2, val3 by rows ? the desired output is as below.

  id  loc vals_mean
1  1 loc1   100
2  2 loc2   15  
1
@T-Heron an example is included here - which part of mcve is your comment referring to ?user3206440
Ok, please disregard now.T-Heron
@T-Heron just in case you down voted ?user3206440
That wasn't from me. I always correct my voting mistakes immediately.T-Heron

1 Answers

4
votes

Your data is not tidy. Tidy it first, using gather:

library(tidyverse)

df %>% 
  gather(key = "key", value = "value", val1, val2, val3) %>% 
  group_by(id, loc) %>% 
  summarize(mean = mean(value, na.rm = T))

#     id    loc  mean
#  <dbl> <fctr> <dbl>
#1     1   loc1   100
#2     2   loc2    15

If you want to keep your dataframe in the nontidy format, rowwise is your friend:

df %>% 
  rowwise() %>% 
  mutate(mean = mean(c(val1, val2, val3), na.rm = TRUE))

## A tibble: 2 × 6
#     id    loc  val1  val2  val3  Mean
#  <dbl> <fctr> <dbl> <dbl> <dbl> <dbl>
#1     1   loc1    10   190    NA   100
#2     2   loc2    20    NA    10    15