0

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.

milos.ai
  • 3,882
  • 7
  • 31
  • 33
Taw
  • 53
  • 6

1 Answers1

2

One idea is to use bind_cols from dplyr as follows,

library(dplyr)
l1 <- split(df$clues, df$resp_ID)
bind_cols(lapply(l1, `length<-`, max(length(l1)))) 

# A tibble: 3 × 3
#      `1`       `2`   `3`
#    <chr>     <chr> <chr>
#1   muddy     green salty
#2   clear elephants clear
#3 insects     muddy  <NA>

NOTE

Setting the length to be equal by compliments of lukeA

Community
  • 1
  • 1
Sotos
  • 51,121
  • 6
  • 32
  • 66
  • great, it worked via temp <- split(df$cluest, df$resp_ID) n <- max(lengths(temp)) new.df<-lapply(temp, `length<-`, n) – Taw Oct 07 '16 at 11:50