Re: [R] create new column with colnames resulting from paste()

2008-06-26 Thread Daniel Folkinshteyn
no need for a for loop - we can vectorize this: > dt <- data.frame(a = c(1, 2, 3), b = c(3, 2, 2), c = c(1, 3, 5)) > dt a b c 1 1 3 1 2 2 2 3 3 3 2 5 > dt[,paste("test", 1:2, sep="")] = rep(1:2, each=3) > dt a b c test1 test2 1 1 3 1 1 2 2 2 2 3 1 2 3 3 2 5 1 2 on 06

Re: [R] create new column with colnames resulting from paste()

2008-06-26 Thread Dong-hyun Oh
Hi, Fortunately, I found a way. -- > dt <- data.frame(a = c(1, 2, 3), b = c(3, 2, 2), c = c(1, 3, 5)) > > for(i in 1:2){ >dt[, paste("test", i, sep = "")] <- rep(i, 3) > } - Thanks. Best, Dong-hyun Oh On Jun 26, 2008, at 6:34 PM, Jorge Ivan Velez

Re: [R] create new column with colnames resulting from paste()

2008-06-26 Thread Jorge Ivan Velez
Dear Dong-hyun, What about dt <- data.frame(a = c(1, 2, 3), b = c(3, 2, 2), c = c(1, 3, 5)) test=matrix(rep(c(1,2),each=3),ncol=2) colnames(test)=paste('test',1:2,sep="") cbind(dt,test) ? HTH, Jorge On Thu, Jun 26, 2008 at 12:23 PM, Dong-hyun Oh <[EMAIL PROTECTED]> wrote: > Dear UseRs, > > I

Re: [R] create new column with colnames resulting from paste()

2008-06-26 Thread jim holtman
Is this what you want > dt <- data.frame(a = c(1, 2, 3), b = c(3, 2, 2), c = c(1, 3, 5)) > for (i in 1:2){ + dt[[paste('test',i,sep="")]] <- rep(i,3) + } > dt a b c test1 test2 1 1 3 1 1 2 2 2 2 3 1 2 3 3 2 5 1 2 > On Thu, Jun 26, 2008 at 12:23 PM, Dong-hyun Oh <[EM

[R] create new column with colnames resulting from paste()

2008-06-26 Thread Dong-hyun Oh
Dear UseRs, I would like to know the way to create a new column by naming it simultaneously. For example, in for() loop I want to create columns named as paste("test", i, sep = ""), as shown below. dt <- data.frame(a = c(1, 2, 3), b = c(3, 2, 2), c = c(1, 3, 5))