Dong-hyun Oh
2008-Jun-26 16:23 UTC
[R] create new column with colnames resulting from paste()
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)) for(i in 1:2){ dt$as.name(paste("test", i, sep = "")) <- rep(i, 3) } -------------------------- Error message of above command is as follows: -------------------------- Error in dt$as.name(paste("test", i, sep = "")) <- rep(i, 3) : target of assignment expands to non-language object -------------------------- The structure of data.frame I want is as follows: > dt a b c test1 test2 1 3 2 1 2 2 2 3 1 2 3 2 5 1 2 Any help? Thank you in advance. Best, ========================================================Dong-hyun Oh Center of Excellence for Science and Innovation Studies Royal Institute or Technology, Sweden e-mail: oh.dongh at gmail.com cel: +46 73 563 45 22
Jorge Ivan Velez
2008-Jun-26 16:34 UTC
[R] create new column with colnames resulting from paste()
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 <r.arecibo@gmail.com> wrote:> 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)) > > for(i in 1:2){ > dt$as.name(paste("test", i, sep = "")) <- rep(i, 3) > } > -------------------------- > > Error message of above command is as follows: > -------------------------- > Error in dt$as.name(paste("test", i, sep = "")) <- rep(i, 3) : > target of assignment expands to non-language object > -------------------------- > > > The structure of data.frame I want is as follows: > > dt > a b c test1 test2 > 1 3 2 1 2 > 2 2 3 1 2 > 3 2 5 1 2 > > Any help? > > Thank you in advance. > > Best, > > > > ========================================================> Dong-hyun Oh > Center of Excellence for Science and Innovation Studies > Royal Institute or Technology, Sweden > e-mail: oh.dongh@gmail.com > cel: +46 73 563 45 22 > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
jim holtman
2008-Jun-26 16:42 UTC
[R] create new column with colnames resulting from paste()
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) + }> dta 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 <r.arecibo at gmail.com> wrote:> 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)) > > for(i in 1:2){ > dt$as.name(paste("test", i, sep = "")) <- rep(i, 3) > } > -------------------------- > > Error message of above command is as follows: > -------------------------- > Error in dt$as.name(paste("test", i, sep = "")) <- rep(i, 3) : > target of assignment expands to non-language object > -------------------------- > > > The structure of data.frame I want is as follows: >> dt > a b c test1 test2 > 1 3 2 1 2 > 2 2 3 1 2 > 3 2 5 1 2 > > Any help? > > Thank you in advance. > > Best, > > > > ========================================================> Dong-hyun Oh > Center of Excellence for Science and Innovation Studies > Royal Institute or Technology, Sweden > e-mail: oh.dongh at gmail.com > cel: +46 73 563 45 22 > > ______________________________________________ > R-help at 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?