I have the following dataframe:
tibble(
people = rep(c("person1", "person2", "person3"), each = 4),
things = rep(c("thing1", "thing2", "thing3", "thing4"), times = 3),
vals = 0) %>%
group_by(people) %>%
mutate(order = seq_along(vals))
|people |things | vals| order|
|:-------|:------|----:|-----:|
|person1 |thing1 | 0| 1|
|person1 |thing2 | 0| 2|
|person1 |thing3 | 0| 3|
|person1 |thing4 | 0| 4|
|person2 |thing1 | 0| 1|
|person2 |thing2 | 0| 2|
|person2 |thing3 | 0| 3|
|person2 |thing4 | 0| 4|
|person3 |thing1 | 0| 1|
|person3 |thing2 | 0| 2|
|person3 |thing3 | 0| 3|
|person3 |thing4 | 0| 4|
I also have list with the "things" that the "people" have done.
# What they did
list(
person1 = c(1, 3, 4),
person2 = c(2, 3),
person3 = NA
)
The values in the list refer to the order column of the original dataframe. So person1 did things 1, 3, and 4, which correspond to the numbers 1, 3, and 4 of the order order column.
I would like to use the list to fill in the column vals based on what each person has done.
The desired output from this example should look like this:
|people |things | vals| order|
|:-------|:------|----:|-----:|
|person1 |thing1 | 1| 1|
|person1 |thing2 | 0| 2|
|person1 |thing3 | 1| 3|
|person1 |thing4 | 1| 4|
|person2 |thing1 | 0| 1|
|person2 |thing2 | 1| 2|
|person2 |thing3 | 1| 3|
|person2 |thing4 | 0| 4|
|person3 |thing1 | 0| 1|
|person3 |thing2 | 0| 2|
|person3 |thing3 | 0| 3|
|person3 |thing4 | 0| 4|
I have tried using base R as well case_when but I can't seem to get my head around how to do this.