Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

apply function to specific columns of data frame

I have a big data frame with n number of columns , now i am trying to do sum of rows of all clumns which start with "col" and exclude the columns which ends with "max".

i am getting a error

data <- data.frame(newid= c(212,132,354,222,263,235,233,263,106,135,144),
                   col1= c(1,2,2,1,4,3,2,4,0,5,0),
                   col2= c(1,1,0,3,2,4,1,0,1,3,5),
                   col3= c(1,1,0,3,2,4,1,0,1,3,5),
                   col4= c(1,1,0,3,2,4,1,0,1,3,5),
                   col_max= c("NM","mn","PA","OH","KL","AB","OT","HA","AM","LA","GA"))


df1 <- df %>% summarise(across(starts_with("col_") &  names(df) != ends_with("_max"), ~ sum(.x, na.rm = TRUE)))

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

df1 <- data %>% summarise(across(starts_with("col") &  !ends_with("_max"), ~ sum(.x, na.rm = TRUE)))

Result:

> df1 <- data %>% summarise(across(starts_with("col") &  !ends_with("_max"), ~ sum(.x, na.rm = TRUE)))
> df1
  col1 col2 col3 col4
1   24   21   21   21

I just modified names(data) != ends_with("_max") to !ends_with("_max").

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading