Olga
Olga

Reputation: 1

Logical Operators in R

My goal is to count how many matches are in Column A and Column B

answer = length(which(DF$A=="English" & which(DF$B=="English")))

I get an error with this attempt. What's the correct way to write this line of code in R?

Upvotes: 0

Views: 66

Answers (1)

kangaroo_cliff
kangaroo_cliff

Reputation: 6222

If you want the number of rows where both A and B has 'English', you can use sum. To get the corresponding row numbers use which as below:

# Number of rows
sum(DF$A == 'English' & DF$B == 'English')
# Row numbers 
which(DF$A == 'English' & DF$B == 'English')

Similarly, if you are interested in the case where at least one of the A and B has this word then use;

sum(DF$A == 'English' | DF$B == 'English')
which(DF$A == 'English' | DF$B == 'English')

Example

DF
#          A       B
# 1  English English
# 2  English English
# 3  English English
# 4  English English
# 5  English English
# 6  English       p
# 7  English       q
# 8  English       r
# 9        i       s
# 10       j       t
    
sum(DF$A == 'English' & DF$B == 'English')
#[1] 5

sum(DF$A == 'English' | DF$B == 'English')
#[1] 8

You can use which to get the row numbers

which(DF$A == 'English' & DF$B == 'English')
# [1] 1 2 3 4 5

which(DF$A == 'English' | DF$B == 'English')
# [1] 1 2 3 4 5 6 7 8

Data

DF <- structure(list(A = c("English", "English", "English", "English", "English", "English", "English", "English", "i", "j"),
                     B = c("English", "English", "English", "English", "English", "p", "q", "r", "s", "t")), class = "data.frame",
                row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"))

Upvotes: 1

Related Questions