Consider the following dataframe:
dummy_df <- tibble(
A=c("ABC", "ABC", "BCD", "CDF", "CDF", "CDF"),
B=c(0.25, 0.25, 1.23, 0.58, 0.58, 0.58),
C=c("lorem", "ipsum", "dolor", "amet", "something", "else"),
D=c("up", "up", "down", "down", "down", "down"),
E=c(132, 132, 243, 512, 512, 512),
F=c("m1", "m1", "m5", "m3", "m3", "m3"),
G=c("val", "val", "mur", "mad", "mad", "mad"),
H=c("grx", "grx", "bcn", "sal", "sal", "sal"),
I=c(1.68, 1.68, 2.31, 3.12, 3.12, 3.12),
J=c("p", "p", "f", "p", "p", "p"),
K=c(100, 100, 200, 143, 143, 143)
)
# A tibble: 6 × 11
A B C D E F G H I J K
<chr> <dbl> <chr> <chr> <dbl> <chr> <chr> <chr> <dbl> <chr> <dbl>
1 ABC 0.25 lorem up 132 m1 val grx 1.68 p 100
2 ABC 0.25 ipsum up 132 m1 val grx 1.68 p 100
3 BCD 1.23 dolor down 243 m5 mur bcn 2.31 f 200
4 CDF 0.58 amet down 512 m3 mad sal 3.12 p 143
5 CDF 0.58 something down 512 m3 mad sal 3.12 p 143
6 CDF 0.58 else down 512 m3 mad sal 3.12 p 143
After reading this, I have managed to collapse column C so that its value is concatenated into a string for every unique row value of column A.
dummy_df %>% group_by(A) %>% summarise(hits = toString(C), nhits=n())
# A tibble: 3 × 3
A hits nhits
<chr> <chr> <int>
1 ABC lorem, ipsum 2
2 BCD dolor 1
3 CDF amet, something, else 3
However, I'm loosing every other column information, which are essential to me. How can I retain information about all the columns while collapsing column C? Ideally it should be done without having to hard-code the columns names, since the number of columns can vary depending on the dataset.
I have read this, but the example shown doesn't create new variables so I haven't been able to make it work.
This is what I'm looking for:
# A tibble: 3 × 12
A hits nhits B D E F G H I J K
<chr> <chr> <int> <dbl> <chr> <dbl> <chr> <chr> <chr> <dbl> <chr> <dbl>
1 ABC lorem, ipsum 2 0.25 up 132 m1 val grx 1.68 p 100
2 BCD dolor 1 1.23 down 243 m5 mur bcn 2.31 f 200
3 CDF amet, something, else 3 0.58 down 512 m3 mad sal 3.12 p 143