Recently I encounter a very interesting question. I want to change the colnames of a model matrix I created.
colnames(model.matrix(~as.factor(c(rep(0,10),1:10))))<-
as.character(sapply(0:10,function(i) paste('sample',i,sep='')))
I keep getting the err "target of assignment expands to non-language object". Finally I was able to work this out. All I do is to assign the model.matrix to a variable first and change its colnames after. Anyone can tell me why the first case didn't work out? Thanks so much
a<-model.matrix(~as.factor(c(rep(0,10),1:10))))
colnames(a) <- as.character(sapply(0:10,function(i) paste('sample',i,sep='')))