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

From: Wolfgang Huber <huber_at_ebi.ac.uk>
Date: Mon, 21 Apr 2008 13:31:20 +0100

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.
>>
>>
> > > ______________________________________________ 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 - 12:33:30 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 - 13:30:31 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