transform
is definitely the wrong tool, I think. And you don't want with
, you probably want within
, in order to return the entire object:
X <- within(X,{contrasts(Cond) <- cbind(c(1,0,-1)/2, c(1,-2,1))/4
contrasts(Age) <- cbind(c(-1,1)/2)
contrasts(Gender) <- cbind(c(-1,1)/2)})
The only tricky part here is to remember the curly braces to enclose multiple lines in a single expression.
Your last example, using attach
, works just fine for me.
transform
is only set up to evaluate expressions of the form tag = value
, and because of the way it evaluates those expressions, it isn't really set up to modify attributes of a column. It is more intended for direct modifications to the columns themselves. (Scaling, taking the log, etc.)
The difference between with
and within
is nicely summed up by the Value section of ?within
:
Value For with, the value of the evaluated expr. For within, the modified object.
So with
only returns the result of the expression. within
is for modifying an object and returning the whole thing.