How to get rowSums for selected columns in R

前端 未结 6 496
余生分开走
余生分开走 2021-01-27 19:30

I am a newbie to R and seek help to calculate sums of selected column for each row. My simple data frame is as below.

data = data.frame(location = c(\"a\",\"b\"         


        
6条回答
  •  野性不改
    2021-01-27 20:05

    We can split the dataset into a list and then use Reduce with f="+".

    sapply(split.default(data[-1], rep(paste0("x", 1:2), each=3)), Reduce, f=`+`)
    #     x1 x2
    #[1,]  14 13
    #[2,]  66 18
    #[3,]   8 12
    #[4,] 100 24
    

提交回复
热议问题