Hello all, I am trying to use cbind to add a named empty column to a Matrix: outputmatrix <- cbind(outputmatrix,kog_id = seq(0,0,0)) The problem I have is that kog_id is a variable that has a value e.g. "KOG1234", but I when I try to use this to name the added column, it is named literally "kog_id" instead of "KOG1234". How can I name a column by passing in a variable for the column name? I am performing this action inside of a for loop, so I can't necessarily know the position of the column in order to name it after it is created. Thanks, Alison Callahan ----------------------- PhD Candidate Department of Biology Carleton University
Try this: `colnames<-`(cbind(m, kog_id = 0), c(colnames(m), kog_id)) On Wed, Oct 13, 2010 at 5:44 PM, Alison Callahan <alison.callahan@gmail.com>wrote:> Hello all, > > I am trying to use cbind to add a named empty column to a Matrix: > > outputmatrix <- cbind(outputmatrix,kog_id = seq(0,0,0)) > > The problem I have is that kog_id is a variable that has a value e.g. > "KOG1234", but I when I try to use this to name the added column, it > is named literally "kog_id" instead of "KOG1234". > > How can I name a column by passing in a variable for the column name? > I am performing this action inside of a for loop, so I can't > necessarily know the position of the column in order to name it after > it is created. > > Thanks, > > Alison Callahan > ----------------------- > PhD Candidate > Department of Biology > Carleton University > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
On Oct 13, 2010, at 4:44 PM, Alison Callahan wrote:> Hello all, > > I am trying to use cbind to add a named empty column to a Matrix: > > outputmatrix <- cbind(outputmatrix,kog_id = seq(0,0,0))Not sure off the top of my head what the right way might be, but this should work: names(outputmatrix)[ which( names(outputmatrix) == "kog_id")] <- kog_id> > The problem I have is that kog_id is a variable that has a value e.g. > "KOG1234", but I when I try to use this to name the added column, it > is named literally "kog_id" instead of "KOG1234". > > How can I name a column by passing in a variable for the column name? > I am performing this action inside of a for loop, so I can't > necessarily know the position of the column in order to name it after > it is created. > > Thanks, > > Alison CallahanDavid Winsemius, MD West Hartford, CT