My challenge is that I want to turn this:
Into:
Would anyone have any idea how to accomplish this? Would be much appreciated. Tried using gather()
but don't really understand it's mechanism!
My challenge is that I want to turn this:
Into:
Would anyone have any idea how to accomplish this? Would be much appreciated. Tried using gather()
but don't really understand it's mechanism!
Either tidyr::gather
or reshape::melt
can transform your data from wide format to long format.
library(tidyr)
gather(data, Type, Specializations, -Person)
library(reshape)
melt(data, id.vars="Person")
Here is a reproducible example that utilizes gather()
from the tidyr
package. For your next questions, please post the code used to create the data frame. In other words, help me help you!
library(tidyverse)
df <- tibble::tribble(
~person, ~s1, ~s2,
"A", "Spec a", "Spec b",
"B", "Spec c", "Spec d",
"C", "Spec e", "Spec f"
)
df %>%
gather(key = "Specialization_number", value = "Specialization", -person) %>%
select(-Specialization_number) %>%
arrange(person)
produces:
# A tibble: 6 x 2
person Specialization
<chr> <chr>
1 A Spec a
2 A Spec b
3 B Spec c
4 B Spec d
5 C Spec e
6 C Spec f