Question: Is there a way to search for multiple conditions in the same column and then count the number of occurrences? 

For example, I want to figure out how many times a specific combination of values (x and then y, x then w, x then z) occur after one another for each respective person. 

I tried writing an IF statement but was told that dplyr would be a better route. 

```
Dataframe: 
c1      c2
person1  x
person1  y
person1  a
person1  a
person2  x
person2  w
person1  x
person1  z

df %>% select(c1, c2) 
   %>% tally(filter(c2 == "x")
     %>% lead(filter(c2=="y")))

```

Expected results: a subset that displays the total number of times x then y, x then w, x then z, appear for each person.

```
c1                 xy            xw          xz
Person 1           1             0           1         
Person 2           0             1           0 

```

R gives me the following error:
===============================

```
  Error in UseMethod("filter_") : 
    no applicable methord for 'filter_' applied to an object of class 
"logical"

```

Answer: 
```
library(dplyr)

c1 = c("person1",
       "person1",
       "person1",
       "person1",       
       "person2",
       "person2",
       "person1", 
       "person1") 

c2 =  c("x","y","a","a","x","w","x","z")

df = as_tibble(cbind(c1,c2))
df %>% 
  group_by(c1)  %>% 
  summarise(xy = sum(c2 == "x" & lead(c2, 1) == "y" ),
            xw = sum(c2 == "x" & lead(c2, 1) == "w"),
            xz = sum(c2 == "x" & lead(c2, 1) == "z"))

```

Gives you

```
# A tibble: 2 x 4
  c1         xy    xw    xz
  <chr>   <int> <int> <int>
1 person1     1     0     1
2 person2     0     1     0

```
An option using `spread`. Create a column by `paste`ing the 'c2' with the `lead` values of 'c2', `filter` only the selected elementss in 'newcol' using `%in%` and `spread` from 'long' to 'wide' format

```
library(tidyverse)
df %>% 
   mutate(newcol = str_c(c2, lead(c2)), ind = 1) %>% 
   select(-c2) %>%
   filter(newcol %in% c('xy', 'xw', 'xz')) %>% 
   spread(newcol, ind, fill = 0)

```

### data

```
df <- structure(list(c1 = c("person1", "person1", "person1", "person1", 
"person2", "person2", "person1", "person1"), c2 = c("x", "y", 
"a", "a", "x", "w", "x", "z")), row.names = c(NA, -8L), class = c("tbl_df", 
"tbl", "data.frame"))

```