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
mcve
is your comment referring to ? – user3206440