use "mean" on some columns of a data frame

hi, i'm trying to calculate the mean of all the rows for some columns in a data frame.
so in the end, i will have a new data frame with the just the columns i want and each column will have just the mean of all its rows.
thanks you !

library(tidyverse)

summarise(mtcars,
          across(c("cyl","disp","hp"),
                 .fns = ~mean(.x,na.rm=TRUE)))

Just to add to nirgrahamuk tidyverse method, te base version would be

rowMeans(mtcars[, c("cyl","disp","hp")])

It works, thanks a lot !!

thank you very much sir !

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.