1

I have a large list, and would like to apply the exact technique detailed in the answer here:

Create mutually exclusive dummy variables from categorical variable in R

However, my data is much larger, and I would like to split, apply and combine the operation to each individual row.

This code, which of course does not work, illustrates what I am trying to do:

id <- c(1,1,1,1)    
time <- c(1,2,3,4)    
time <- as.character(time)    
unique.time <- as.character(unique(df$time))    
df <- data.frame(id,time)    
df1 <- split(df, row(df))    
sapply(df1, (unique.time, function(x)as.numeric(df1$time == x)))    
z <- unsplit(lapply(df1, row(df)), scale), x)

Thanks!

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • So what exactly is the question here? What's the desired output? – MrFlick Aug 17 '17 at 18:38
  • What @MrFlick said and also, read the help page for `lapply/sapply`. Your use is syntatically wrong. It should be `sapply(df1, function(x) ...etc...)`. And the same with `lapply(df1, scale)`. – Rui Barradas Aug 17 '17 at 18:47
  • It's quite odd to split based on rows. If you really want to split that way, then you can use df1 <- split(df, rownames(df)). I would suggest going the apply method route or using a for loop to loop through the rows. – Katie Aug 17 '17 at 18:50
  • The question would be to get output in the same format as: https://stackoverflow.com/questions/30622422/create-mutually-exclusive-dummy-variables-from-categorical-variable-in-r But my list is much larger, and due to memory constraints, I cannot use loops. So how do I apply the above technique for a much larger list? It seemed splitting by rows, then applying the transformation would make sense. But can you recommend any other means? – Navin Kumar Aug 17 '17 at 22:02

0 Answers0