-1

My challenge is that I want to turn this:

original

Into:

desired

Would anyone have any idea how to accomplish this? Would be much appreciated. Tried using gather() but don't really understand it's mechanism!

Jaap
  • 81,064
  • 34
  • 182
  • 193
  • sorry, stack exchange does not allow me to embed images here yet – pushmandate Oct 04 '17 at 10:55
  • `rbind()` can help – abhiieor Oct 04 '17 at 10:55
  • Please post the data into the question so that we dont have to type it of of the image. – guscht Oct 04 '17 at 11:08
  • Welcome to StackOverflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This will make it much easier for others to help you. – Jaap Oct 04 '17 at 11:20
  • All of your comments are noted. Thank you for your patience! – pushmandate Oct 05 '17 at 02:07

2 Answers2

0

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")
Djork
  • 3,319
  • 1
  • 16
  • 27
  • Thank you R.S.! This is surprisingly elegant considering the code I was trying to write up. Got a lot to learn :) – pushmandate Oct 05 '17 at 02:07
0

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
Davis Vaughan
  • 2,780
  • 9
  • 19
  • Thank you so much, Davis. Your comment about uploading code for the dataset is well-noted. I should have thought of that! In any case, much appreciated! – pushmandate Oct 05 '17 at 02:05