[R] modifying colnames of tables in a loop

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Thu Nov 16 16:32:09 CET 2006


try the following (untested):

tb1 <- tb2 <- tb3 <- matrix(1:100, 10, 10, dim = list(NULL, 
LETTERS[1:10]))

lis <- list("tb1", "tb2", "tb3")
for(i in seq(lis)){
    tb <- get(lis[[i]])
    colnames(tb) <- tolower(colnames(tb))
    assign(lis[[i]], tb)
}


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Werner Wernersen" <pensterfuzzer at yahoo.de>
To: "Benilton Carvalho" <bcarvalh at jhsph.edu>
Cc: <r-help at stat.math.ethz.ch>
Sent: Thursday, November 16, 2006 4:06 PM
Subject: Re: [R] modifying colnames of tables in a loop


> That's almost perfect! Only the operation
> colnames(get(x)) <-
> seems not to be defined.
>
> Now I can't figure out how to assign the colnames of
> the object in the original environment.
>
> Thanks, Benilton!
>
> --- Benilton Carvalho <bcarvalh at jhsph.edu> schrieb:
>
>> how about:
>>
>> for (x in l) colnames(get(x)) <-
>> lower.case(colnames(get(x)))
>>
>> b
>>
>> On Nov 16, 2006, at 9:01 AM, Werner Wernersen wrote:
>>
>> > Hi,
>> >
>> > I have a list with the names of tables, e.g.
>> > l <- c("t1","t2","t3")
>> > and I want to change the colnames of each of the
>> > tables in a for loop like this:
>> > for (x in l) {
>> >     colnames(eval(x)) <-
>> lower.case(colnames(eval(x)))
>> > }
>> >
>> > This does not work but could someone give me some
>> help
>> > to get on the right track?
>> >
>> > Thanks a million,
>> >   Werner
>> >
>> > ______________________________________________
>> > R-help at stat.math.ethz.ch 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 stat.math.ethz.ch 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.
> 


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



More information about the R-help mailing list