0

I am trying to pass a vector of columns through rowSums, where the data set is:

Services <- c("prv_serv_1", "prv_serv_2", "prv_serv_3")
Patient_ID <- c("A", "B" , "C", "D")
Obs_1 <-c(0, 1, 1)
Obs_2 <-c(1, 1, 0)
Obs_3 <-c(0, 0, 1)
Obs_4 <-c(0, 1, 0)

#matrix

Services_Matrix <-rbind(Obs_1, Obs_2, Obs_3, Obs_4)
rm(Obs_1, Obs_2, Obs_3, Obs_4)
colnames(Services_Matrix) <- Services
rownames(Services_Matrix) <- Patient_ID

#set up vector of columns 
prv_services <- c("prv_serv_1", "prv_serv_2")

I then seek to append the sum of the rows for only the columns in the vector, so the output is, but I am trying to find a way to pass prv_services through the rowSums function.

matrix_sums=cbind(services_matrix,rowSums)
matrix_sums     
      serv_1  serv_2 serv_3 rowSums  
obs_1  0        1     1       1
obs_2  1        1     0       2
obs_3  0        0     1       0
obs_4  0        1     0       1
four77
  • 49
  • 4

0 Answers0