serenade
serenade

Reputation: 359

Sum of Rows in List in R

I want to find rows in which sum of rows give 3 among these columns: 2nd (Q-1_A-4), 3th (Q-4_A-1) and 5th (Q-9_A-1). Input and desired output is at the below.

Input:

ID   Q-1_A-4   Q-4_A-1   Q-6_A-1  Q-9_A-1

122       1       1        0       1
123       0       1        1       0
124       0       0        1       0 
125       0       0        0       0   
126       1       1        1       1 

Desired output:

ID no 122 and 126 gives 3 as a row sum for these 3 columns: 
(2nd (Q-1_A-4), 3th (Q-4_A-1) and 5th (Q-9_A-1))

Upvotes: 0

Views: 1042

Answers (1)

akrun
akrun

Reputation: 887028

We can use rowSums on a subset of columns, check if the sum is 3, and use that to subset the rows of 'df1'.

df1[rowSums(df1[c(2,3,5)])==3,]

Upvotes: 2

Related Questions