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

From: Dong-hyun Oh <r.arecibo_at_gmail.com>
Date: Thu, 26 Jun 2008 18:23:40 +0200


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. Received on Thu 26 Jun 2008 - 16:27:41 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 26 Jun 2008 - 17:30:54 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive