Collapses all the unique values of a character variable in a (long-format) data frame with a given separator and within a given group. It is also possible to define values that need to be excluded from the collapse; any rows with these excluded values will be left as is.
Usage
collapse_column_vals(
data,
column = "event_name",
exclude = "",
group_by = c("subject_id", "item_group"),
separator = ", "
)
Arguments
- data
A data frame in long format.
- column
Character string. Column containing the strings to collapse.
- exclude
Character vector. Values that should not be collapsed.
- group_by
Character vector. Collapsing will occur grouped by these variables.
- separator
Character string to collapse the column values with.
Examples
library(dplyr)
#>
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#>
#> filter, lag
#> The following objects are masked from ‘package:base’:
#>
#> intersect, setdiff, setequal, union
df <- data.frame(
ID = sample(1:5, 15, replace = TRUE),
name = sample(c("collapsed", "excluded", "names"), 15, replace = TRUE)
) |>
mutate(
site = sample(c("S1", "S2", "S3"), 1),
.by = ID
) |>
arrange(ID, factor(name))
collapse_column_vals(df, column = "name", exclude = "excluded",
group_by = c("ID", "site"))
#> ID name site
#> 1 1 excluded S2
#> 2 2 names S1
#> 3 3 collapsed, names S2
#> 4 3 collapsed, names S2
#> 5 4 excluded S1
#> 6 4 excluded S1
#> 7 4 names S1
#> 8 5 collapsed, names S2
#> 9 5 collapsed, names S2
#> 10 5 excluded S2
#> 11 5 excluded S2
#> 12 5 excluded S2
#> 13 5 collapsed, names S2
#> 14 5 collapsed, names S2
#> 15 5 collapsed, names S2