Svalf Svalf - 3 months ago 10
R Question

Using R: Subset rows meeting specific conditions using for loop

I have the following data frame:

ID A1 A2 A3 A4 B1 B2 B3 B4
1 ID1 1 2 1 1 1 1 2 2
2 ID2 2 2 1 1 2 NA 2 1
3 ID3 2 2 1 2 1 1 NA 2
4 ID4 1 1 1 1 1 NA NA 2
5 ID5 2 2 1 1 NA NA 2 NA
6 ID6 1 1 1 1 2 2 2 2


I want to extract rows from the data frame meeting specific conditions for each combination of A and B. These are the conditions:


  • PP: value 2 present in A-type column + value 2 present in B-type column

  • PA: value 2 present in A-type column + value 1 present in B-type column

  • AP: value 1 present in A-type column + value 2 present in B-type column

  • AA: value 1 present in A-type column + value 1 present in B-type column



This is what I wrote:

A <- colnames(dataframe)[2:5]
# A = A1, A2, A3 and A4
B <- colnames(dataframe)[6:9]
# B = B1, B2, B3 and B4

for (a in A) {
for (b in B) {
PP <- dataframe[dataframe$a=='2' & dataframe$b=='2' , ]
PA <- dataframe[dataframe$a=='2' & dataframe$b=='1' , ]
AP <- dataframe[dataframe$a=='1' & dataframe$b=='2' , ]
AA <- dataframe[dataframe$a=='1' & dataframe$b=='1' , ]
print(head(PP)) #to have a preview
}
}


However the new data frames are empty and I don't understand why.
Ideally, the first for loop (with a=A1 and b=B1) would therefore output:

PP:

ID A1 A2 A3 A4 B1 B2 B3 B4
2 ID2 2 2 1 1 2 NA 2 1


PA:

ID A1 A2 A3 A4 B1 B2 B3 B4
3 ID3 2 2 1 2 1 1 NA 2


AP:

ID A1 A2 A3 A4 B1 B2 B3 B4
6 ID6 1 1 1 1 2 2 2 2


AA:

ID A1 A2 A3 A4 B1 B2 B3 B4
1 ID1 1 2 1 1 1 1 2 2


I hope someone can help.
Thanks.

Answer

The dollar sign extraction is creating a problem. It is not meant for passing of variables as in dataframe$a. Because a is not the actual name. You are trying to pass the variable name on to it. But that operator will look for the literal a column and not find it. Try dataframe[,a]