Your problem is that in
is not already a variable in your data frame (I changed the name to inval
because there are a few weirdnesses associated with trying to use a variable called in
inside within
).
I generated inval
by using zoo::na.locf
to set the value for each row to the last previously specified value:
library(zoo)
df <- within(df,{
inval <- ifelse(var=="in",value,NA)
inval <- na.locf(inval)
})
This results in:
str(df)
## 'data.frame': 16 obs. of 4 variables:
## $ convID: Factor w/ 2 levels "aa","ab": 1 2 1 2 1 2 1 2 1 2 ...
## $ var : Factor w/ 4 levels "ic","id","in",..: 3 3 2 2 4 4 1 1 3 3 ...
## $ value : chr "1" "1" "4/29/14" "4/20/14" ...
## $ inval : chr "1" "1" "1" "1" ...
Then it's easy to dcast
:
library(reshape2)
dcast(subset(df,var!="in"),convID+inval~...)