I would like to perform row-wise summary statistics in R.
I tried adapting this solution (https://stackoverflow.com/a/68443762/3251466) to use mean, but am getting strange results.
df1 <- tibble(foo = c(1,5,2), bar = c(10,1,3), foobar = c(6,3,5))
df1 %>% rowwise() %>% mutate(s = sum(!!!syms(colnames(.)))) #works
df1 %>% rowwise() %>% mutate(m = mean(!!!syms(colnames(.)))) #just returns foo
df1 %>% rowwise() %>% mutate(sd = sd(!!!syms(colnames(.)))) #fails
# Attempting with an NA
df2 <- tibble(foo = c(1,5,2), bar = c(10,NA,3), foobar = c(6,3,5))
df2 %>% rowwise() %>% mutate(s = sum(!!!syms(colnames(.)), na.rm=T)) #works
df2 %>% rowwise() %>% mutate(m = mean(!!!syms(colnames(.)), na.rm=T)) #this time mean fails
Any idea why this fails?
I assume things like apply and rowMeans will work (I have yet to look into those).
I was hoping for a more generic method than rowMeans though so that I could perform other row-wise analyses and was curious if there was a tidyr alternative to apply.
Thanks!
CodePudding user response:
The generic approach you could adopt to perform rowwise operations is via c_across
i.e.
df1 |> rowwise() %>% mutate(s=sum(c_across()))
df1 |> rowwise() %>% mutate(m=mean(c_across()))
df1 |> rowwise() %>% mutate(sd=sd(c_across()))