I have a dataset that looks like this:
phrase wo1sp wo2sp wo3sp wo1sc wo2sc wo3sc
hello dan mark todd 10 5 4
hello mark dan chris 8 9 4
goodbye mark dan kev 2 4 10
what kev dan mark 4 5 5
And I'd like to change it to something like this:
phrase sp sc
hello dan 10
hello mark 5
hello todd 4
hello mark 8
hello dan 9
hello chris 4
goodbye mark 2
goodbye dan 4
goodbye kev 10
what kev 4
what dan 5
what mark 5
Many of the suggestions I've come across don't depend on the data columns being coupled with one another—so the suggestions end up losing information in things like gather
calls. I asked this question yesterday.
I've solved it like this:
library("tidyverse")
test_set = tribble(~phrase, ~wo1sp, ~wo2sp, ~wo3sp, ~wo1sc, ~wo2sc, ~wo3sc,
"hello", "dan", "mark", "todd", 10, 5, 4,
"goodbye", "mark", "dan", "kev", 2, 4, 10,
"what", "kev", "dan", "mark", 4, 5, 5,
"hello", "mark", "dan", "mark", 4, 7, 10)
tmp_list <- list()
for (ii in 1:3) {
selected_data <- test_set %>%
group_by(phrase) %>%
select(matches(paste("wo", ii, "|phrase", sep="")))
names(selected_data) <- c("phrase", "sp", "sc")
tmp_list[[ii]] <- selected_data
}
ds <- do.call(rbind, tmp_list)
Which gives me exactly what I want, but it feels... hacky. In R, I try to avoid loops and messing around with column names (until the final cleanup) as much as I can. Even the select
statement feels funny. I'm confident there's a cleaner way of doing this, but I've spent a very long time with (mostly with tidyr
) trying to figure out how, and have come up with nothing.
Does anyone have any ideas?
(The duplicated question seems to be the same, but the answer to this question is radically different [and way cooler])