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)))
>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")
.