Skip to content
Prev 326834 / 398502 Next

creating dummy variables based on conditions

Hello,

Your data seems to be of class 'matrix'. The following code needs it to 
be a data.frame.

dat <- as.data.frame(your input matrix)

res <- do.call(rbind, lapply(split(dat, list(dat$id, dat$year)), 
function(x){
	x$ans <- if(any(x$var == 1)) 1 else 0
	x}))
rownames(res) <- NULL
res


Hope this helps,

Rui Barradas

Em 14-07-2013 12:30, Anup Nandialath escreveu: