Re: [R] change column names of several data frames

From: David Scott <d.scott_at_auckland.ac.nz>
Date: Tue, 22 Apr 2008 10:09:44 +1200 (NZST)

On Mon, 21 Apr 2008, Henrik Parn wrote:

> Dear all,
>
> I have several data frames for which I want to change the column names.
>
> Example data:
> data.1 <- data.frame(x1 = rnorm(5))
> data.2 <- data.frame(x1 = rnorm(5))
> .
> .
>
>
> What I want to achieve:
> names(data.1) <- "y1"
> names(data.1) <- "y1"
> .
> .
>
>
> Is it possible to achieve this with a loop or any of the apply-functions?
>
>
> Some (out of several...) unsuccessful attempts using for-loops instead:
> for(i in 1:2)
> names(get(paste("data", i, sep = "."))) <- "y1"
>
> for(i in 1:2)
> assign(paste("data", i, sep="."), names(get(paste("natal", i, sep =
> "."))) <- "y1")
>

I think what you are missing is as.name.

I think you need (untested) something like:

names(as.name(paste("data",i,sep="."))) <- "y1"

David Scott


David Scott	Department of Statistics, Tamaki Campus
 		The University of Auckland, PB 92019
 		Auckland 1142,    NEW ZEALAND
Phone: +64 9 373 7599 ext 86830		Fax: +64 9 373 7000
Email:	d.scott_at_auckland.ac.nz

Graduate Officer, Department of Statistics Director of Consulting, Department of Statistics



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 Mon 21 Apr 2008 - 22:12:59 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 Mon 21 Apr 2008 - 22:30:35 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