问题
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
So, I know the first thing to do here is group_by(phrase)
. What I'm not sure about is how to associate sp1
with sc1
, sp2
with sc2
, etc. and make those each into their own rows. I've seen some vaguely similar things using reshape
and tidy
, but they don't depend on there being coupled columns. I would basically just like to collapse the numbers in the column names.
I have a request: When you answer, would you mind explaining what the code itself does? Many of the things I've searched on StackExchange present a seemingly esoteric solution with no explanation of what's going on.
回答1:
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)
test_set %>%
gather(key = col, value = val, -phrase) %>%
separate(col = col, into = c("num", "suffix"), sep = 3) %>%
spread(key = suffix, value = val) %>%
mutate(sc = as.numeric(sc)) %>%
select(-num)
Edit: I guess it's not necessary to split col
into three columns, could just do sep = 3
来源:https://stackoverflow.com/questions/53840788/how-can-i-go-from-wide-to-long-while-coupling-numbered-columns-with-one-another