change column names of several data frames

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

change column names of several data frames

Henrik Parn
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")


Thanks in advance!


/ Henrik Pärn

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: change column names of several data frames

Antonio, Fabio Di Narzo
Henrik Parn <henrik.parn <at> bio.ntnu.no> writes:

>
> 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))

Use lists. I.e.:
data <- list()
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")
>
> Thanks in advance!
>
> / Henrik Pärn
>
> ______________________________________________
> 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.
>
>



--
Antonio, Fabio Di Narzo
Ph.D. student at
Department of Statistical Sciences
University of Bologna, Italy

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: change column names of several data frames

Wolfgang Huber

Hi Henrik,

afaIcs this should work:

for(v in sprintf("data.%d", 1:n)) {
  f = get(v)
  names(f) = whatever
  assign(v, f)
}

--
Best wishes
 Wolfgang

------------------------------------------------------------------
Wolfgang Huber  EBI/EMBL  Cambridge UK  http://www.ebi.ac.uk/huber



21/04/2008 13:10 Antonio, Fabio Di Narzo a écrit

> Henrik Parn <henrik.parn <at> bio.ntnu.no> writes:
>
>> 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))
>
> Use lists. I.e.:
> data <- list()
> 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")
>>
>> Thanks in advance!
>>
>> / Henrik Pärn
>>
>> ______________________________________________
>> 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.
>>
>>
>
>
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: change column names of several data frames

David Scott-6
In reply to this post by Henrik Parn
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: [hidden email]

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

______________________________________________
[hidden email] 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.