I'm using the grepl function to try and sort through data; all the row numbers are different survey respondents, and each number in the "ANI_type" string represents a different type of animal - I need to sort these depending on animal type. For example, the "2"s under ANI_type represent cats. I thought I had it figured out with the following, but it's not only including the "2", but any digit that contains a "2" as well. How can I get this to work so that it ONLY includes "2"? Thanks so much, I'm incredibly new at this!
> animals$cats <- as.numeric(grepl("2", animals$ANI_type))
> animals
ANI_type dogs cats repamp
1 1,2,5,12,13,14,15,16,18,19,27 1 1 TRUE
2 2 0 1 FALSE
3 20,21,22,23,26 1 1 TRUE
4 20,21,22,23 1 1 TRUE
5 13 1 0 TRUE
6 2 0 1 FALSE
7 20,21,22 1 1 TRUE
8 20,21,22,23 1 1 TRUE
9 20,21,22 1 1 TRUE
10 5,20,21,22,27 1 1 TRUE
11 1,2,20,21,22 1 1 TRUE
12 5,18,20,21,22,23,26 1 1 TRUE
13 20,21 1 1 TRUE
14 21 1 1 TRUE
15 20,21 1 1 TRUE
16 20,21,26 1 1 TRUE
17 2 0 1 FALSE
18 1,2 1 1 TRUE
19 2 0 1 FALSE
20 3,4 0 0 FALSE
Furthermore, I need to group some of the digits in the strings into categories. For example, digits 6,7,8,9,10,11 all need to be placed in the animals$pock object. How would I go about that using the grep function? Just use alot of the boundary tokens?