I'm looking to code a new variable for multiple rows in a group based on one row in the group and not finding a working solution.
Minimal working example:
df <- data.frame("participant" = c("1", "1", "1", "1", "1", "1", "1", "1",
"2", "2", "2", "2", "2", "2", "2", "2"),
"group" = c("1111", "1111", "1111", "1111", "1113", "1113", "1113", "1114",
"1111", "1111", "1113", "1113", "1113", "1113", "1113", "1113"),
"item" = c("a", "b", "c", "d", "e", "f", "g", "h",
"i", "j", "k", "l", "m", "n", "o", "p"),
"value" = c("xyz", "hlm", "test", "nop", "test", "nop", "hlm", "test",
"hlm", "test", "xyz", "xyz", "test", "xyz", "nop", "xyz"),
"type" = c("1", "2", "1", "2", "2", "1", "1", "2",
"1", "1", "2", "2", "2", "3", "1", "1"))
I want to 1) group by participant and group, 2) check the type of each item which has value == "test", then 3) code a new column test_type which spreads the type of each "test" across all rows within the same participant/group combination.
The final result should look like this:
df$test_type <- c("1", "1", "1", "1", "2", "2", "2", "2",
"1", "1", "2", "2", "2", "2", "2", "2")
Any tips?