sum cells of certain columns for each row

前端 未结 3 1366
既然无缘
既然无缘 2020-12-10 02:03

I would like to calculate sums for certain columns and then apply this summation for every row. Unfortunately, I can only get to the first step. How do I now make it happen

3条回答
  •  难免孤独
    2020-12-10 02:25

    The summation of all individual rows can also be done using the row-wise operations of dplyr (with col1, col2, col3 defining three selected columns for which the row-wise sum is calculated):

    library(tidyverse)
    
    df <- df %>% 
        rowwise() %>% 
        mutate(rowsum = sum(c(col1, col2,col3)))
    

提交回复
热议问题