I am trying to covert my data format to be able to use it in another software. In my case I need to convert the levels of resp into separate variables while keeping the list of clues for each respID. My data is as follows
df<-structure(list(resp_ID = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L), clues = structure(c(5L, 1L, 4L, 3L, 2L, 5L, 6L, 1L), .Label = c("clear", "elephants",
"green", "insects", "muddy", "salty"), class = "factor")), .Names = c("resp_ID",
"clues"), class = "data.frame", row.names = c(NA, -8L))
df
resp_ID clues
1 1 muddy
2 1 clear
3 1 insects
4 2 green
5 2 elephants
6 2 muddy
7 3 salty
8 3 clear
#I want the resulting data to be like
output<-structure(list(X1 = structure(c(3L, 1L, 2L), .Label = c("clear",
"insects", "muddy"), class = "factor"), X2 = structure(c(2L,
1L, 3L), .Label = c("elephants", "green", "muddy"), class = "factor"),
X3 = structure(c(3L, 2L, 1L), .Label = c("", "clear", "salty"
), class = "factor")), .Names = c("X1", "X2", "X3"), class = "data.frame", row.names = c(NA,
-3L))
output
X1 X2 X3
1 muddy green salty
2 clear elephants clear
3 insects muddy
>
I tried to use (!!table(cbind(df[1],stack(df[1])[2])))
but I think i am getting the ordering wrong somewhere and also tried using the libary(caret)
without success.