Sore Finger Tips
Sore Finger Tips

Reputation: 343

R: Count rows with string matches in two columns

I have a file named "gamedata" with 30 columns (a mix of strings, numbers, and logicals), I need to count the number of rows where a string in column "home" matches "Seahawks" AND a partial string match in column "penalty" matches "*holding*".

All I need is a count of the total number of rows where (gamedata$home == "Seahawks" & gamedata$penalty == "*holding*").

I've tried several wrong solutions, which simply result in integer(0), when I can see that there are several valid instances. Your help is appreciated.

Upvotes: 1

Views: 3970

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520888

Try using sum(expression):

sum(gamedata$home == "Seahawks" & grepl("holding", gamedata$penalty))

Upvotes: 3

Related Questions