I'm sure this must be a duplicate. Say I have this:
stage <- seq(1,3,1)
exp1 <- c("A","B","C")
exp2 <- c("A","B","C")
exp3 <- c(NA, "B","C")
exp4 <- c("D","B","C")
exp5 <- c("A","B","C")
exp6 <- c(NA, "B","C")
df <- data.frame(stage, exp1, exp2, exp3, exp4, exp5, exp6)
stage exp1 exp2 exp3 exp4 exp5 exp6
1 1 A A <NA> D A <NA>
2 2 B B B B B B
3 3 C C C C C C
I want to combine all duplicated columns and show this by concatenating the column names. I can find duplicated columns with:
df[duplicated(lapply(df, summary))]
exp2 exp5 exp6
1 A A <NA>
2 B B B
3 C C C
But I can't figure out how to combine the duplicated column names such that I get something like this:
stage exp1_exp2_exp5 exp3_exp6 exp4
1 1 A <NA> D
2 2 B B B
3 3 C C C
Perhaps (probably) this is a poor way to visualize a comparison between different "exp"s? Maybe I need to reshape to show this more clearly?