r row-wide conditional replacement -
friends
i'm trying t set matrix
or data.frame
canonical correlation analysis. original dataset has column designating 1 of x conditions , subsequent columns of explanatory variables. need set array sets indicator variable each condition "x". eg. columns in df are:
id cond task1 taskn a, x, 12, 14 b, x, 13, 17 c, y, 11, 10 d, z, 10, 13
here "cond" can x,y,z,... (can vary, don't know how many). needs go to:
id, x, y, z, task1, taskn a, 1, 0, 0, 12, 14 b, 1, 0, 0, 13, 17 c, 0, 1, 0, 11, 10 d, 0, 0, 1, 10, 13
so, can set indicators in array
iv<-as.data.frame(array(,c(nrow(df),length(levels(cond)))))
and cbind
df, can't figure out how go array , set appropriate indicator "1" , rest "0".
any suggestions?
thanks
jon
if code cond
factor, can r expansion want via model.matrix
. complication coding chose (dummy variables coding, or sum contrasts in r) need change default constrasts used r's model formula code.
## data dat <- data.frame(id = letters[1:4], cond = factor(c("x","x","y","z")), task1 = c(12,13,11,10), taskn = c(14,17,10,13)) dat ## r produce dummy variables us, ## coding needs contr.sum contrasts op <- options(contrasts = c("contr.sum","contr.poly")) dat2 <- data.frame(id = dat$id, model.matrix(id ~ . - 1, data = dat)) ## levels of cond lev <- with(dat, levels(cond)) ## fix-up names names(dat2)[2:(1+length(lev))] <- lev dat2 ## reset contrasts options(op)
this gives us:
> dat2 id x y z task1 taskn 1 1 0 0 12 14 2 b 1 0 0 13 17 3 c 0 1 0 11 10 4 d 0 0 1 10 13
this should scale automatically number of levels in cond
changes/increases.
hth
Comments
Post a Comment