I have a data frame:
df <- data.frame( Otherspp = c("suck SD", "BT", "SD RS", "RSS"), Dominantspp = c("OM", "OM", "RSS", "CH"), Commonspp = c(" ", " ", " ", "OM"), Rarespp = c(" ", " ", "SD", "NP"), NP = rep("northern pikeminnow|NORTHERN PIKEMINNOW|np|NP|npm|NPM", 4), OM = rep("steelhead|STEELHEAD|rainbow trout|RAINBOW TROUT|st|ST|rb|RB|om|OM", 4), RSS = rep("redside shiner|REDSIDE SHINER|rs|RS|rss|RSS", 4), suck = rep("suckers|SUCKERS|sucker|SUCKER|suck|SUCK|su|SU|ss|SS", 4) )
I need to use the columns populated with common fish codes/names (NP, OM, RSS, suck) to evaluate the expressions in the first four columns and output a 1/0 based on each of those columns, if the expression is met EXACTLY. The code I have below does not match full words (only partial) and provides incorrect data (see resulting tibble below).
df %>% rowwise() %>% transmute_at(vars(NP, OM, RSS, suck), funs(case_when( grepl(., Dominantspp) ~ "1", grepl(., Commonspp) ~ "1", grepl(., Rarespp) ~ "1", grepl(., Otherspp) ~ "1", TRUE ~ "0"))) %>% ungroup()
Result: see that in row three, both "suck" and "RSS" receive a "1".
# A tibble: 4 x 4 NP OM RSS suck <chr> <chr> <chr> <chr> 1 0 1 0 1 2 0 1 0 0 3 0 0 1 1 4 1 1 1 1
Desired output:
NP OM RSS suck 1 0 1 0 1 2 0 1 0 0 3 0 0 1 0 4 1 1 1 0