Sore Finger Tips Sore Finger Tips - 1 year ago 110
R Question

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.

Answer Source

Try using sum(expression):

sum(gamedata$home == "Seahawks" & grepl("holding", gamedata$penalty))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download