-1

I am working on survey data with 512 variables. The amount of variables is high because each respondent answers the same question for multiple brands. So each variable, for example 'preference' has 20 columns, for each brand a respondent could possible know. I want one additional column, which is brand, and I did not manage this yet.

I applied the tidyr package and this works very well for the first set of variables:

TestSet100<-as.data.frame(TestSet100)
TestSet100$ID <- factor(TestSet100$ID)
Test_long<-reshape(TestSet100long, varying=c(unaided_b1:unaided_b20), direction="long", idvar= "ID", sep="_")

The result is exactly what I need. But, it goes wrong as soon as I start with the next one:

TestSet100long3<-gather(TestSet100long, brand, aided_awareness, aided_b1:aided_b20, factor_key=T)

Because then it adds: brand unaided_awareness, brand aided_awareness, in stead of one column with the variable brand, and two with unaided_awareness and aided_awareness.

But with the third one, it goes totally wrong, because then the result is an error.

TestSet100long4<-gather(TestSet100long3, brand, familiarity, fami_b1:fami_b20, factor_key=T)
Error: is_dictionaryish(x) is not TRUE
halfer
  • 19,824
  • 17
  • 99
  • 186
Moniek
  • 11
  • 1
  • 1
    Can you provide a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) of your dataset? It would make it easier for us to troubleshoot your code. – Z.Lin Sep 01 '17 at 12:42

1 Answers1

0

Try dcast from reshape2 package. See An introduction to reshape2 for more details. At the bottom of this blogpost there is a nice visual table with blue/red highlightings nicely explaining the logic

user1766682
  • 400
  • 3
  • 14