str_c()1. Combine subtests based on id and
status
Review the data (d8)
# A tibble: 6 x 3
id status subtest
<dbl> <chr> <chr>
1 1 complete test1
2 1 pending test2
3 1 complete test3
4 2 pending test1
5 2 complete test2
6 2 complete test3
To combine subtests in long format, I first need to group the data by
both id and status using
dplyr::group_by().
Then I can create a new variable “subtest” which will be my combined
subtests. I can use my stringr::str_c() function to combine
or concatenate the subtests. I want subtests to be separated by a “,” so
I will add the argument collapse = “,”
d8_new <- d8 %>%
dplyr::group_by(id, status) %>%
dplyr::summarize(subtest = stringr::str_c(subtest, collapse=","))
d8_new
# A tibble: 4 x 3
# Groups: id [2]
id status subtest
<dbl> <chr> <chr>
1 1 complete test1,test3
2 1 pending test2
3 2 complete test2,test3
4 2 pending test1
If I wanted all of a user’s information on the same row, I could take
this one step further and use tidyr::pivot_wider() to pivot
the data to wide format.
d8_new %>%
tidyr::pivot_wider(
names_from = status,
values_from = subtest
)
# A tibble: 2 x 3
# Groups: id [2]
id complete pending
<dbl> <chr> <chr>
1 1 test1,test3 test2
2 2 test2,test3 test1
Return to Strings