Similar to unite() do to columns, is there a way to combine rows across columns for specific rows separating values with semicolon?
In the example below, IC_1 and IC_2 were combined as a new row, values between brackets and separeted by ;
structure(list(treatment = c("product", "product", "product",
"product", "control", "control", "control", "control"), variable = c("A",
"B", "IC_1", "IC_2", "A", "B", "IC_1", "IC_2"), X1 = 1:8, X2 = 8:15,
X3 = 16:23), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-8L))
treatment variable X1 X2 X3
<chr> <chr> <int> <int> <int>
1 product A 1 8 16
2 product B 2 9 17
3 product IC_1 3 10 18
4 product IC_2 4 11 19
5 control A 5 12 20
6 control B 6 13 21
7 control IC_1 7 14 22
8 control IC_2 8 15 23
Desired output:
treatment variable X1 X2 X3
<chr> <chr> <chr> <chr> <chr>
1 product A 1 8 16
2 product B 2 9 17
3 product IC [3;4][10;11][18:19]
4 control A 5 12 20
5 control B 6 13 21
6 control IC [7;8][14;15][22;23]