Search code examples
rdplyrsummarizeacross

Assigning the result of an across(starts_with(), mean) to the R dataframe


Instead of writing manually an average of

df$average<- (df$a + df$b + df$c)/3

I wanted to use across, but I can't manage to attach it because then creates a 1x1 dataframe I was trying variations of this without success.

df$`mean trust` <- df%>% 
    summarise(across(starts_with("Trust"), mean, na.rm = TRUE))

and also

df$`mean trust` <- df%>%
  summarise(across(starts_with("Trust"), 
                   mean, na.rm=TRUE,
                   .names = "`mean trust`"))

I also prefer to use %<>% to assign, if that's possible at all. Any suggestions?


Solution

  • We can use rowMeans and instead of summarise, it would be mutate

    library(dplyr)
    df %>% 
         mutate(meantrust = select(., starts_with("Trust")) %>%
                         rowMeans( na.rm = TRUE))
    

    With c_across, we can use mean (but it is not vectorized)

    df %>%
        rowwise %>% 
        mutate(meantrust = mean(c_across(starts_with("Trust")), na.rm = TRUE))
    

    A reproducible example with iris

    data(iris)
    head(iris) %>%
        rowwise %>%
         mutate(meanSepal = mean(c_across(starts_with("Sepal")), na.rm = TRUE))