I'm currently working with some data and really struggle to properly transform it from wide to long data. For reference, this was data in the beginning: data before
Unfortunately, I only learned to do gather () with one key variable, which would produce way to many rows when run several times with different keys, but tried it after looking it up, but it went wrong and I can't find my error. Can someone please help?
I managed to transform it to something close to what I want with the following code:
l <- p %>% gather(column, value, 4:11, 14:25) %>%
separate (column, into = c("column", "obj_number", "familiarity")) %>%
spread (column, value)
l$obj_num = ifelse ((l$obj_number == 'fam') | (l$obj_number == 'nov'), NA, l$obj_number)
Unfortunately, it somehow extracted the names of the objects (car, bear, bus, iguana, seal, wagon) weirdly. I don't know how to make an additional variable out of this. data now
This is a picture of how I imagine my dataframe to look like in the end: data I want
I am wondering what went wrong with my code and if one can run a transformation from wide to long data with multiple variables. simultaneously tried to work from there, but both approaches failed.