changing column labels for data frames inside a list

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

changing column labels for data frames inside a list

Vikram Chhatre
> summary(mygenfreqt)
                  Length Class  Mode
dat1.str 59220  -none- numeric
dat2.str 59220  -none- numeric
dat3.str 59220  -none- numeric

> head(mylist[[1]])
           1     2     3     4     5     6     7     8     9    10    11
 12
L0001.1 0.60 0.500 0.325 0.675 0.600 0.500 0.500 0.375 0.550 0.475 0.350
0.275
L0001.2 0.40 0.500 0.675 0.325 0.400 0.500 0.500 0.625 0.450 0.525 0.650
0.725

I want to change 1:12 to pop1:pop12

mylist<- lapply(mylist, function(e) colnames(e) <- paste0('pop',1:12))

What this is doing is replacing the data frames with just names
pop1:pop12.  I just want to replace the column labels.

Thanks for any suggestions.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: changing column labels for data frames inside a list

Sarah Goslee
colnames(e) <- paste0('pop',1:12)

isn't a function and doesn't return anything.

> mylist <- list(
+ data.frame(a = runif(10), b = runif(10)),
+ data.frame(c = runif(10), d = runif(10)),
+ data.frame(e = runif(10), f = runif(10)))
> mylist2 <- lapply(mylist, function(e){colnames(e) <- paste0('pop', 1:2); e})
> colnames(mylist2[[1]])
[1] "pop1" "pop2"

Sarah

On Mon, Mar 30, 2015 at 9:54 AM, Vikram Chhatre
<[hidden email]> wrote:

>> summary(mygenfreqt)
>                   Length Class  Mode
> dat1.str 59220  -none- numeric
> dat2.str 59220  -none- numeric
> dat3.str 59220  -none- numeric
>
>> head(mylist[[1]])
>            1     2     3     4     5     6     7     8     9    10    11
>  12
> L0001.1 0.60 0.500 0.325 0.675 0.600 0.500 0.500 0.375 0.550 0.475 0.350
> 0.275
> L0001.2 0.40 0.500 0.675 0.325 0.400 0.500 0.500 0.625 0.450 0.525 0.650
> 0.725
>
> I want to change 1:12 to pop1:pop12
>
> mylist<- lapply(mylist, function(e) colnames(e) <- paste0('pop',1:12))
>
> What this is doing is replacing the data frames with just names
> pop1:pop12.  I just want to replace the column labels.
>
> Thanks for any suggestions.
>

--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: changing column labels for data frames inside a list

MacQueen, Don
In reply to this post by Vikram Chhatre
Assuming that the elements of mylist are data frames, try this:

mylist <- lapply(mylist, function(e) { names(e) <- paste0('pop',1:12) ; e})


With certain exceptions, the result of a function is the result of the
last expression in the function body. As you defined it, the last
expression was
  colnames(e) <- paste0('pop',1:12)
that is, the column names (not "labels", but names).

If the elements really are data frames, then names() can be used instead
of colnames(), but colnames() is ok. I don't know if one of them is better
than the other for data frames.

-Don

--
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 3/30/15, 6:54 AM, "Vikram Chhatre" <[hidden email]> wrote:

>> summary(mygenfreqt)
>                  Length Class  Mode
>dat1.str 59220  -none- numeric
>dat2.str 59220  -none- numeric
>dat3.str 59220  -none- numeric
>
>> head(mylist[[1]])
>           1     2     3     4     5     6     7     8     9    10    11
> 12
>L0001.1 0.60 0.500 0.325 0.675 0.600 0.500 0.500 0.375 0.550 0.475 0.350
>0.275
>L0001.2 0.40 0.500 0.675 0.325 0.400 0.500 0.500 0.625 0.450 0.525 0.650
>0.725
>
>I want to change 1:12 to pop1:pop12
>
>mylist<- lapply(mylist, function(e) colnames(e) <- paste0('pop',1:12))
>
>What this is doing is replacing the data frames with just names
>pop1:pop12.  I just want to replace the column labels.
>
>Thanks for any suggestions.
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>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 -- To UNSUBSCRIBE and more, see
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: changing column labels for data frames inside a list

Ivan Calandra-4
In reply to this post by Vikram Chhatre
I am not sure it would do it since there is no reproducible example, but
try names() instead of colnames().

HTH,
Ivan

--
Ivan Calandra, ATER
University of Reims Champagne-Ardenne
GEGENAA - EA 3795
CREA - 2 esplanade Roland Garros
51100 Reims, France
+33(0)3 26 77 36 89
[hidden email]
https://www.researchgate.net/profile/Ivan_Calandra

Le 30/03/15 15:54, Vikram Chhatre a écrit :

>> summary(mygenfreqt)
>                    Length Class  Mode
> dat1.str 59220  -none- numeric
> dat2.str 59220  -none- numeric
> dat3.str 59220  -none- numeric
>
>> head(mylist[[1]])
>             1     2     3     4     5     6     7     8     9    10    11
>   12
> L0001.1 0.60 0.500 0.325 0.675 0.600 0.500 0.500 0.375 0.550 0.475 0.350
> 0.275
> L0001.2 0.40 0.500 0.675 0.325 0.400 0.500 0.500 0.625 0.450 0.525 0.650
> 0.725
>
> I want to change 1:12 to pop1:pop12
>
> mylist<- lapply(mylist, function(e) colnames(e) <- paste0('pop',1:12))
>
> What this is doing is replacing the data frames with just names
> pop1:pop12.  I just want to replace the column labels.
>
> Thanks for any suggestions.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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: changing column labels for data frames inside a list

Sven E. Templer
In reply to this post by Sarah Goslee
On 30 March 2015 at 16:47, Sarah Goslee <[hidden email]> wrote:

> colnames(e) <- paste0('pop',1:12)
>
> isn't a function and doesn't return anything.
>

But
function(e){colnames(e) <- paste0('pop', 1:2)}
is a function and it returns something (the last evaluated expression! -
here the paste0 return):

> mylist2 <- lapply(mylist, function(e){colnames(e) <- paste0('pop', 1:2)})
> mylist2
[[1]]
[1] "pop1" "pop2"

[[2]]
[1] "pop1" "pop2"

[[3]]
[1] "pop1" "pop2"

 from ?return:

If the end of a function is reached without calling return, the value of
the last evaluated expression is returned.

>
> > mylist <- list(
> + data.frame(a = runif(10), b = runif(10)),
> + data.frame(c = runif(10), d = runif(10)),
> + data.frame(e = runif(10), f = runif(10)))
> > mylist2 <- lapply(mylist, function(e){colnames(e) <- paste0('pop', 1:2);
> e})
> > colnames(mylist2[[1]])
> [1] "pop1" "pop2"
>
> Sarah
>
> On Mon, Mar 30, 2015 at 9:54 AM, Vikram Chhatre
> <[hidden email]> wrote:
> >> summary(mygenfreqt)
> >                   Length Class  Mode
> > dat1.str 59220  -none- numeric
> > dat2.str 59220  -none- numeric
> > dat3.str 59220  -none- numeric
> >
> >> head(mylist[[1]])
> >            1     2     3     4     5     6     7     8     9    10    11
> >  12
> > L0001.1 0.60 0.500 0.325 0.675 0.600 0.500 0.500 0.375 0.550 0.475 0.350
> > 0.275
> > L0001.2 0.40 0.500 0.675 0.325 0.400 0.500 0.500 0.625 0.450 0.525 0.650
> > 0.725
> >
> > I want to change 1:12 to pop1:pop12
> >
> > mylist<- lapply(mylist, function(e) colnames(e) <- paste0('pop',1:12))
> >
> > What this is doing is replacing the data frames with just names
> > pop1:pop12.  I just want to replace the column labels.
> >
> > Thanks for any suggestions.
> >
>
> --
> Sarah Goslee
> http://www.functionaldiversity.org
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: changing column labels for data frames inside a list

Vikram Chhatre
First of all, thank you for all the quick replies.  Here is a solution that
worked for me.

mylist2 <- lapply(mylist, function(e){colnames(e) <- paste0('pop',1:12);
return(e)})

> head(mylist2[[1]])
        pop1  pop2  pop3  pop4  pop5  pop6  pop7  pop8  pop9 pop10 pop11
pop12
L0001.1 0.60 0.500 0.325 0.675 0.600 0.500 0.500 0.375 0.550 0.475 0.350
0.275
L0001.2 0.40 0.500 0.675 0.325 0.400 0.500 0.500 0.625 0.450 0.525 0.650
0.725

While we are at this, I wanted to create a 13th column in each data frame
for average of each row.

# Calculate average
myavg <- lapply(mylist2, function(e) rowSums(mylist2)/12)

# Attach to the main data frame
mylist3 <- cbind(mylist2, myavg)

This does not work the way I imagined it would.  The myavg vector is
attached directly to mylist2, not to individual dataframes within.

p.s. Is it a standard convention to always copy the reply to the last
person who responded?

On Mon, Mar 30, 2015 at 10:56 AM, Sven E. Templer <[hidden email]>
wrote:

> On 30 March 2015 at 16:47, Sarah Goslee <[hidden email]> wrote:
>
> > colnames(e) <- paste0('pop',1:12)
> >
> > isn't a function and doesn't return anything.
> >
>
> But
> function(e){colnames(e) <- paste0('pop', 1:2)}
> is a function and it returns something (the last evaluated expression! -
> here the paste0 return):
>
> > mylist2 <- lapply(mylist, function(e){colnames(e) <- paste0('pop', 1:2)})
> > mylist2
> [[1]]
> [1] "pop1" "pop2"
>
> [[2]]
> [1] "pop1" "pop2"
>
> [[3]]
> [1] "pop1" "pop2"
>
>  from ?return:
>
> If the end of a function is reached without calling return, the value of
> the last evaluated expression is returned.
>
> >
> > > mylist <- list(
> > + data.frame(a = runif(10), b = runif(10)),
> > + data.frame(c = runif(10), d = runif(10)),
> > + data.frame(e = runif(10), f = runif(10)))
> > > mylist2 <- lapply(mylist, function(e){colnames(e) <- paste0('pop',
> 1:2);
> > e})
> > > colnames(mylist2[[1]])
> > [1] "pop1" "pop2"
> >
> > Sarah
> >
> > On Mon, Mar 30, 2015 at 9:54 AM, Vikram Chhatre
> > <[hidden email]> wrote:
> > >> summary(mygenfreqt)
> > >                   Length Class  Mode
> > > dat1.str 59220  -none- numeric
> > > dat2.str 59220  -none- numeric
> > > dat3.str 59220  -none- numeric
> > >
> > >> head(mylist[[1]])
> > >            1     2     3     4     5     6     7     8     9    10
> 11
> > >  12
> > > L0001.1 0.60 0.500 0.325 0.675 0.600 0.500 0.500 0.375 0.550 0.475
> 0.350
> > > 0.275
> > > L0001.2 0.40 0.500 0.675 0.325 0.400 0.500 0.500 0.625 0.450 0.525
> 0.650
> > > 0.725
> > >
> > > I want to change 1:12 to pop1:pop12
> > >
> > > mylist<- lapply(mylist, function(e) colnames(e) <- paste0('pop',1:12))
> > >
> > > What this is doing is replacing the data frames with just names
> > > pop1:pop12.  I just want to replace the column labels.
> > >
> > > Thanks for any suggestions.
> > >
> >
> > --
> > Sarah Goslee
> > http://www.functionaldiversity.org
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > 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.
> >
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: changing column labels for data frames inside a list

Bert Gunter
In reply to this post by Sven E. Templer
Sarah's statement is correct.

So is yours. They are not contradictory, and I believe Sarah's point
was that the OP needed to learn the appropriate syntax.

-- Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
Clifford Stoll




On Mon, Mar 30, 2015 at 7:56 AM, Sven E. Templer <[hidden email]> wrote:

> On 30 March 2015 at 16:47, Sarah Goslee <[hidden email]> wrote:
>
>> colnames(e) <- paste0('pop',1:12)
>>
>> isn't a function and doesn't return anything.
>>
>
> But
> function(e){colnames(e) <- paste0('pop', 1:2)}
> is a function and it returns something (the last evaluated expression! -
> here the paste0 return):
>
>> mylist2 <- lapply(mylist, function(e){colnames(e) <- paste0('pop', 1:2)})
>> mylist2
> [[1]]
> [1] "pop1" "pop2"
>
> [[2]]
> [1] "pop1" "pop2"
>
> [[3]]
> [1] "pop1" "pop2"
>
>  from ?return:
>
> If the end of a function is reached without calling return, the value of
> the last evaluated expression is returned.
>
>>
>> > mylist <- list(
>> + data.frame(a = runif(10), b = runif(10)),
>> + data.frame(c = runif(10), d = runif(10)),
>> + data.frame(e = runif(10), f = runif(10)))
>> > mylist2 <- lapply(mylist, function(e){colnames(e) <- paste0('pop', 1:2);
>> e})
>> > colnames(mylist2[[1]])
>> [1] "pop1" "pop2"
>>
>> Sarah
>>
>> On Mon, Mar 30, 2015 at 9:54 AM, Vikram Chhatre
>> <[hidden email]> wrote:
>> >> summary(mygenfreqt)
>> >                   Length Class  Mode
>> > dat1.str 59220  -none- numeric
>> > dat2.str 59220  -none- numeric
>> > dat3.str 59220  -none- numeric
>> >
>> >> head(mylist[[1]])
>> >            1     2     3     4     5     6     7     8     9    10    11
>> >  12
>> > L0001.1 0.60 0.500 0.325 0.675 0.600 0.500 0.500 0.375 0.550 0.475 0.350
>> > 0.275
>> > L0001.2 0.40 0.500 0.675 0.325 0.400 0.500 0.500 0.625 0.450 0.525 0.650
>> > 0.725
>> >
>> > I want to change 1:12 to pop1:pop12
>> >
>> > mylist<- lapply(mylist, function(e) colnames(e) <- paste0('pop',1:12))
>> >
>> > What this is doing is replacing the data frames with just names
>> > pop1:pop12.  I just want to replace the column labels.
>> >
>> > Thanks for any suggestions.
>> >
>>
>> --
>> Sarah Goslee
>> http://www.functionaldiversity.org
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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: changing column labels for data frames inside a list

Sven E. Templer
In reply to this post by Vikram Chhatre
On 30 March 2015 at 17:19, Vikram Chhatre <[hidden email]> wrote:

> First of all, thank you for all the quick replies.  Here is a solution that
> worked for me.
>
> mylist2 <- lapply(mylist, function(e){colnames(e) <- paste0('pop',1:12);
> return(e)})
>
> > head(mylist2[[1]])
>         pop1  pop2  pop3  pop4  pop5  pop6  pop7  pop8  pop9 pop10 pop11
> pop12
> L0001.1 0.60 0.500 0.325 0.675 0.600 0.500 0.500 0.375 0.550 0.475 0.350
> 0.275
> L0001.2 0.40 0.500 0.675 0.325 0.400 0.500 0.500 0.625 0.450 0.525 0.650
> 0.725
>
> While we are at this, I wanted to create a 13th column in each data frame
> for average of each row.
>

For new problems you should use a new topic.


>
> # Calculate average
> myavg <- lapply(mylist2, function(e) rowSums(mylist2)/12)
>
> # Attach to the main data frame
> mylist3 <- cbind(mylist2, myavg)
>

> This does not work the way I imagined it would.  The myavg vector is
> attached directly to mylist2, not to individual dataframes within.
>
>
But it works as expected (read ?cbind).
You try to cbind two lists (myavg and mylist2).
You want to cbind each list object (the data.frames) with each rowSums
output.
So, use cbind within your first lapply.

p.s. Is it a standard convention to always copy the reply to the last
> person who responded?
>

I guess it depends on which answer you refer to.


>
> On Mon, Mar 30, 2015 at 10:56 AM, Sven E. Templer <[hidden email]>
> wrote:
>
> > On 30 March 2015 at 16:47, Sarah Goslee <[hidden email]> wrote:
> >
> > > colnames(e) <- paste0('pop',1:12)
> > >
> > > isn't a function and doesn't return anything.
> > >
> >
> > But
> > function(e){colnames(e) <- paste0('pop', 1:2)}
> > is a function and it returns something (the last evaluated expression! -
> > here the paste0 return):
> >
> > > mylist2 <- lapply(mylist, function(e){colnames(e) <- paste0('pop',
> 1:2)})
> > > mylist2
> > [[1]]
> > [1] "pop1" "pop2"
> >
> > [[2]]
> > [1] "pop1" "pop2"
> >
> > [[3]]
> > [1] "pop1" "pop2"
> >
> >  from ?return:
> >
> > If the end of a function is reached without calling return, the value of
> > the last evaluated expression is returned.
> >
> > >
> > > > mylist <- list(
> > > + data.frame(a = runif(10), b = runif(10)),
> > > + data.frame(c = runif(10), d = runif(10)),
> > > + data.frame(e = runif(10), f = runif(10)))
> > > > mylist2 <- lapply(mylist, function(e){colnames(e) <- paste0('pop',
> > 1:2);
> > > e})
> > > > colnames(mylist2[[1]])
> > > [1] "pop1" "pop2"
> > >
> > > Sarah
> > >
> > > On Mon, Mar 30, 2015 at 9:54 AM, Vikram Chhatre
> > > <[hidden email]> wrote:
> > > >> summary(mygenfreqt)
> > > >                   Length Class  Mode
> > > > dat1.str 59220  -none- numeric
> > > > dat2.str 59220  -none- numeric
> > > > dat3.str 59220  -none- numeric
> > > >
> > > >> head(mylist[[1]])
> > > >            1     2     3     4     5     6     7     8     9    10
> > 11
> > > >  12
> > > > L0001.1 0.60 0.500 0.325 0.675 0.600 0.500 0.500 0.375 0.550 0.475
> > 0.350
> > > > 0.275
> > > > L0001.2 0.40 0.500 0.675 0.325 0.400 0.500 0.500 0.625 0.450 0.525
> > 0.650
> > > > 0.725
> > > >
> > > > I want to change 1:12 to pop1:pop12
> > > >
> > > > mylist<- lapply(mylist, function(e) colnames(e) <-
> paste0('pop',1:12))
> > > >
> > > > What this is doing is replacing the data frames with just names
> > > > pop1:pop12.  I just want to replace the column labels.
> > > >
> > > > Thanks for any suggestions.
> > > >
> > >
> > > --
> > > Sarah Goslee
> > > http://www.functionaldiversity.org
> > >
> > > ______________________________________________
> > > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > > 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.
> > >
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > 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.
> >
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: changing column labels for data frames inside a list

Sven E. Templer
In reply to this post by Bert Gunter
On 30 March 2015 at 17:31, Bert Gunter <[hidden email]> wrote:

> Sarah's statement is correct.
>
> So is yours. They are not contradictory, and I believe Sarah's point
> was that the OP needed to learn the appropriate syntax.
>
>
That's why I pointed to ?return.
Sarah's statement was not so clear (and might have been misleading) for me
regarding the R expertise of the OP.


> -- Bert
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
> (650) 467-7374
>
> "Data is not information. Information is not knowledge. And knowledge
> is certainly not wisdom."
> Clifford Stoll
>
>
>
>
> On Mon, Mar 30, 2015 at 7:56 AM, Sven E. Templer <[hidden email]>
> wrote:
> > On 30 March 2015 at 16:47, Sarah Goslee <[hidden email]> wrote:
> >
> >> colnames(e) <- paste0('pop',1:12)
> >>
> >> isn't a function and doesn't return anything.
> >>
> >
> > But
> > function(e){colnames(e) <- paste0('pop', 1:2)}
> > is a function and it returns something (the last evaluated expression! -
> > here the paste0 return):
> >
> >> mylist2 <- lapply(mylist, function(e){colnames(e) <- paste0('pop',
> 1:2)})
> >> mylist2
> > [[1]]
> > [1] "pop1" "pop2"
> >
> > [[2]]
> > [1] "pop1" "pop2"
> >
> > [[3]]
> > [1] "pop1" "pop2"
> >
> >  from ?return:
> >
> > If the end of a function is reached without calling return, the value of
> > the last evaluated expression is returned.
> >
> >>
> >> > mylist <- list(
> >> + data.frame(a = runif(10), b = runif(10)),
> >> + data.frame(c = runif(10), d = runif(10)),
> >> + data.frame(e = runif(10), f = runif(10)))
> >> > mylist2 <- lapply(mylist, function(e){colnames(e) <- paste0('pop',
> 1:2);
> >> e})
> >> > colnames(mylist2[[1]])
> >> [1] "pop1" "pop2"
> >>
> >> Sarah
> >>
> >> On Mon, Mar 30, 2015 at 9:54 AM, Vikram Chhatre
> >> <[hidden email]> wrote:
> >> >> summary(mygenfreqt)
> >> >                   Length Class  Mode
> >> > dat1.str 59220  -none- numeric
> >> > dat2.str 59220  -none- numeric
> >> > dat3.str 59220  -none- numeric
> >> >
> >> >> head(mylist[[1]])
> >> >            1     2     3     4     5     6     7     8     9    10
> 11
> >> >  12
> >> > L0001.1 0.60 0.500 0.325 0.675 0.600 0.500 0.500 0.375 0.550 0.475
> 0.350
> >> > 0.275
> >> > L0001.2 0.40 0.500 0.675 0.325 0.400 0.500 0.500 0.625 0.450 0.525
> 0.650
> >> > 0.725
> >> >
> >> > I want to change 1:12 to pop1:pop12
> >> >
> >> > mylist<- lapply(mylist, function(e) colnames(e) <- paste0('pop',1:12))
> >> >
> >> > What this is doing is replacing the data frames with just names
> >> > pop1:pop12.  I just want to replace the column labels.
> >> >
> >> > Thanks for any suggestions.
> >> >
> >>
> >> --
> >> Sarah Goslee
> >> http://www.functionaldiversity.org
> >>
> >> ______________________________________________
> >> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> >> 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.
> >>
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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
|

Fwd: changing column labels for data frames inside a list

Bert Gunter
In reply to this post by Vikram Chhatre
Sorry, I failed to cc the list.

-- Bert





---------- Forwarded message ----------
From: Bert Gunter <[hidden email]>
Date: Mon, Mar 30, 2015 at 8:36 AM
Subject: Re: [R] changing column labels for data frames inside a list
To: Vikram Chhatre <[hidden email]>


You really really need to spend (more?) time with a good R tutorial
before posting further. Your questions are entirely due to your
ignorance of standard language syntax that you should learn if you
intend to use R. Of course, feel free to ask for help if there are
places in the tutorials where, after an honest effort, you remain
flummoxed.

Cheers,
Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
Clifford Stoll




On Mon, Mar 30, 2015 at 8:19 AM, Vikram Chhatre
<[hidden email]> wrote:

> First of all, thank you for all the quick replies.  Here is a solution that
> worked for me.
>
> mylist2 <- lapply(mylist, function(e){colnames(e) <- paste0('pop',1:12);
> return(e)})
>
>> head(mylist2[[1]])
>         pop1  pop2  pop3  pop4  pop5  pop6  pop7  pop8  pop9 pop10 pop11
> pop12
> L0001.1 0.60 0.500 0.325 0.675 0.600 0.500 0.500 0.375 0.550 0.475 0.350
> 0.275
> L0001.2 0.40 0.500 0.675 0.325 0.400 0.500 0.500 0.625 0.450 0.525 0.650
> 0.725
>
> While we are at this, I wanted to create a 13th column in each data frame
> for average of each row.
>
> # Calculate average
> myavg <- lapply(mylist2, function(e) rowSums(mylist2)/12)
>
> # Attach to the main data frame
> mylist3 <- cbind(mylist2, myavg)
>
> This does not work the way I imagined it would.  The myavg vector is
> attached directly to mylist2, not to individual dataframes within.
>
> p.s. Is it a standard convention to always copy the reply to the last
> person who responded?
>
> On Mon, Mar 30, 2015 at 10:56 AM, Sven E. Templer <[hidden email]>
> wrote:
>
>> On 30 March 2015 at 16:47, Sarah Goslee <[hidden email]> wrote:
>>
>> > colnames(e) <- paste0('pop',1:12)
>> >
>> > isn't a function and doesn't return anything.
>> >
>>
>> But
>> function(e){colnames(e) <- paste0('pop', 1:2)}
>> is a function and it returns something (the last evaluated expression! -
>> here the paste0 return):
>>
>> > mylist2 <- lapply(mylist, function(e){colnames(e) <- paste0('pop', 1:2)})
>> > mylist2
>> [[1]]
>> [1] "pop1" "pop2"
>>
>> [[2]]
>> [1] "pop1" "pop2"
>>
>> [[3]]
>> [1] "pop1" "pop2"
>>
>>  from ?return:
>>
>> If the end of a function is reached without calling return, the value of
>> the last evaluated expression is returned.
>>
>> >
>> > > mylist <- list(
>> > + data.frame(a = runif(10), b = runif(10)),
>> > + data.frame(c = runif(10), d = runif(10)),
>> > + data.frame(e = runif(10), f = runif(10)))
>> > > mylist2 <- lapply(mylist, function(e){colnames(e) <- paste0('pop',
>> 1:2);
>> > e})
>> > > colnames(mylist2[[1]])
>> > [1] "pop1" "pop2"
>> >
>> > Sarah
>> >
>> > On Mon, Mar 30, 2015 at 9:54 AM, Vikram Chhatre
>> > <[hidden email]> wrote:
>> > >> summary(mygenfreqt)
>> > >                   Length Class  Mode
>> > > dat1.str 59220  -none- numeric
>> > > dat2.str 59220  -none- numeric
>> > > dat3.str 59220  -none- numeric
>> > >
>> > >> head(mylist[[1]])
>> > >            1     2     3     4     5     6     7     8     9    10
>> 11
>> > >  12
>> > > L0001.1 0.60 0.500 0.325 0.675 0.600 0.500 0.500 0.375 0.550 0.475
>> 0.350
>> > > 0.275
>> > > L0001.2 0.40 0.500 0.675 0.325 0.400 0.500 0.500 0.625 0.450 0.525
>> 0.650
>> > > 0.725
>> > >
>> > > I want to change 1:12 to pop1:pop12
>> > >
>> > > mylist<- lapply(mylist, function(e) colnames(e) <- paste0('pop',1:12))
>> > >
>> > > What this is doing is replacing the data frames with just names
>> > > pop1:pop12.  I just want to replace the column labels.
>> > >
>> > > Thanks for any suggestions.
>> > >
>> >
>> > --
>> > Sarah Goslee
>> > http://www.functionaldiversity.org
>> >
>> > ______________________________________________
>> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> > 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.
>> >
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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: changing column labels for data frames inside a list

Sarah Goslee
In reply to this post by Sven E. Templer
On Mon, Mar 30, 2015 at 11:43 AM, Sven E. Templer
<[hidden email]> wrote:

>
>
> On 30 March 2015 at 17:31, Bert Gunter <[hidden email]> wrote:
>>
>> Sarah's statement is correct.
>>
>> So is yours. They are not contradictory, and I believe Sarah's point
>> was that the OP needed to learn the appropriate syntax.
>>
>
> That's why I pointed to ?return.
> Sarah's statement was not so clear (and might have been misleading) for me
> regarding the R expertise of the OP.

You're right: I totally wasn't clear. I got involved making a working
reproducible example and didn't explain what I was doing. And you are
totally correct, the last expression will be returned. Mea culpa.

Sarah


>>
>> -- Bert
>>
>> Bert Gunter
>> Genentech Nonclinical Biostatistics
>> (650) 467-7374
>>
>> "Data is not information. Information is not knowledge. And knowledge
>> is certainly not wisdom."
>> Clifford Stoll
>>
>>
>>
>>
>> On Mon, Mar 30, 2015 at 7:56 AM, Sven E. Templer <[hidden email]>
>> wrote:
>> > On 30 March 2015 at 16:47, Sarah Goslee <[hidden email]> wrote:
>> >
>> >> colnames(e) <- paste0('pop',1:12)
>> >>
>> >> isn't a function and doesn't return anything.
>> >>
>> >
>> > But
>> > function(e){colnames(e) <- paste0('pop', 1:2)}
>> > is a function and it returns something (the last evaluated expression! -
>> > here the paste0 return):
>> >
>> >> mylist2 <- lapply(mylist, function(e){colnames(e) <- paste0('pop',
>> >> 1:2)})
>> >> mylist2
>> > [[1]]
>> > [1] "pop1" "pop2"
>> >
>> > [[2]]
>> > [1] "pop1" "pop2"
>> >
>> > [[3]]
>> > [1] "pop1" "pop2"
>> >
>> >  from ?return:
>> >
>> > If the end of a function is reached without calling return, the value of
>> > the last evaluated expression is returned.
>> >
>> >>
>> >> > mylist <- list(
>> >> + data.frame(a = runif(10), b = runif(10)),
>> >> + data.frame(c = runif(10), d = runif(10)),
>> >> + data.frame(e = runif(10), f = runif(10)))
>> >> > mylist2 <- lapply(mylist, function(e){colnames(e) <- paste0('pop',
>> >> > 1:2);
>> >> e})
>> >> > colnames(mylist2[[1]])
>> >> [1] "pop1" "pop2"
>> >>
>> >> Sarah
>> >>
>> >> On Mon, Mar 30, 2015 at 9:54 AM, Vikram Chhatre
>> >> <[hidden email]> wrote:
>> >> >> summary(mygenfreqt)
>> >> >                   Length Class  Mode
>> >> > dat1.str 59220  -none- numeric
>> >> > dat2.str 59220  -none- numeric
>> >> > dat3.str 59220  -none- numeric
>> >> >
>> >> >> head(mylist[[1]])
>> >> >            1     2     3     4     5     6     7     8     9    10
>> >> > 11
>> >> >  12
>> >> > L0001.1 0.60 0.500 0.325 0.675 0.600 0.500 0.500 0.375 0.550 0.475
>> >> > 0.350
>> >> > 0.275
>> >> > L0001.2 0.40 0.500 0.675 0.325 0.400 0.500 0.500 0.625 0.450 0.525
>> >> > 0.650
>> >> > 0.725
>> >> >
>> >> > I want to change 1:12 to pop1:pop12
>> >> >
>> >> > mylist<- lapply(mylist, function(e) colnames(e) <-
>> >> > paste0('pop',1:12))
>> >> >
>> >> > What this is doing is replacing the data frames with just names
>> >> > pop1:pop12.  I just want to replace the column labels.
>> >> >
>> >> > Thanks for any suggestions.
>> >> >
>> >>
>> >> --
>> >> Sarah Goslee
>> >> http://www.functionaldiversity.org

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: changing column labels for data frames inside a list

Sven E. Templer
On 30 March 2015 at 17:50, Sarah Goslee <[hidden email]> wrote:

> On Mon, Mar 30, 2015 at 11:43 AM, Sven E. Templer
> <[hidden email]> wrote:
> >
> >
> > On 30 March 2015 at 17:31, Bert Gunter <[hidden email]> wrote:
> >>
> >> Sarah's statement is correct.
> >>
> >> So is yours. They are not contradictory, and I believe Sarah's point
> >> was that the OP needed to learn the appropriate syntax.
> >>
> >
> > That's why I pointed to ?return.
> > Sarah's statement was not so clear (and might have been misleading) for
> me
> > regarding the R expertise of the OP.
>
> You're right: I totally wasn't clear. I got involved making a working
> reproducible example and didn't explain what I was doing. And you are
> totally correct, the last expression will be returned. Mea culpa.
>
>
No one to blame but the missing reproducible example, I agree!

Sven


> Sarah
>
>
> >>
> >> -- Bert
> >>
> >> Bert Gunter
> >> Genentech Nonclinical Biostatistics
> >> (650) 467-7374
> >>
> >> "Data is not information. Information is not knowledge. And knowledge
> >> is certainly not wisdom."
> >> Clifford Stoll
> >>
> >>
> >>
> >>
> >> On Mon, Mar 30, 2015 at 7:56 AM, Sven E. Templer <
> [hidden email]>
> >> wrote:
> >> > On 30 March 2015 at 16:47, Sarah Goslee <[hidden email]>
> wrote:
> >> >
> >> >> colnames(e) <- paste0('pop',1:12)
> >> >>
> >> >> isn't a function and doesn't return anything.
> >> >>
> >> >
> >> > But
> >> > function(e){colnames(e) <- paste0('pop', 1:2)}
> >> > is a function and it returns something (the last evaluated
> expression! -
> >> > here the paste0 return):
> >> >
> >> >> mylist2 <- lapply(mylist, function(e){colnames(e) <- paste0('pop',
> >> >> 1:2)})
> >> >> mylist2
> >> > [[1]]
> >> > [1] "pop1" "pop2"
> >> >
> >> > [[2]]
> >> > [1] "pop1" "pop2"
> >> >
> >> > [[3]]
> >> > [1] "pop1" "pop2"
> >> >
> >> >  from ?return:
> >> >
> >> > If the end of a function is reached without calling return, the value
> of
> >> > the last evaluated expression is returned.
> >> >
> >> >>
> >> >> > mylist <- list(
> >> >> + data.frame(a = runif(10), b = runif(10)),
> >> >> + data.frame(c = runif(10), d = runif(10)),
> >> >> + data.frame(e = runif(10), f = runif(10)))
> >> >> > mylist2 <- lapply(mylist, function(e){colnames(e) <- paste0('pop',
> >> >> > 1:2);
> >> >> e})
> >> >> > colnames(mylist2[[1]])
> >> >> [1] "pop1" "pop2"
> >> >>
> >> >> Sarah
> >> >>
> >> >> On Mon, Mar 30, 2015 at 9:54 AM, Vikram Chhatre
> >> >> <[hidden email]> wrote:
> >> >> >> summary(mygenfreqt)
> >> >> >                   Length Class  Mode
> >> >> > dat1.str 59220  -none- numeric
> >> >> > dat2.str 59220  -none- numeric
> >> >> > dat3.str 59220  -none- numeric
> >> >> >
> >> >> >> head(mylist[[1]])
> >> >> >            1     2     3     4     5     6     7     8     9    10
> >> >> > 11
> >> >> >  12
> >> >> > L0001.1 0.60 0.500 0.325 0.675 0.600 0.500 0.500 0.375 0.550 0.475
> >> >> > 0.350
> >> >> > 0.275
> >> >> > L0001.2 0.40 0.500 0.675 0.325 0.400 0.500 0.500 0.625 0.450 0.525
> >> >> > 0.650
> >> >> > 0.725
> >> >> >
> >> >> > I want to change 1:12 to pop1:pop12
> >> >> >
> >> >> > mylist<- lapply(mylist, function(e) colnames(e) <-
> >> >> > paste0('pop',1:12))
> >> >> >
> >> >> > What this is doing is replacing the data frames with just names
> >> >> > pop1:pop12.  I just want to replace the column labels.
> >> >> >
> >> >> > Thanks for any suggestions.
> >> >> >
> >> >>
> >> >> --
> >> >> Sarah Goslee
> >> >> http://www.functionaldiversity.org
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: changing column labels for data frames inside a list

MacQueen, Don
In reply to this post by Vikram Chhatre
Regarding the averages, someone else mentioned that it's preferred to
start a new question in a new post to the list.

That said, you are confusing "inside" the list with "outside" the list.
Try this:

(the following R expression is supposed to be all on one line, but my
email software may cause a line break)

myavgs <- lapply(mylist2, function(e) {cbind( e, avgs=rowSums(e)/12)} )

With no example data I can't test it. The brackets {} are not, strictly
speaking, necessary, but I think they help clarify what is inside the
function with what is outside it.

--
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 3/30/15, 8:19 AM, "Vikram Chhatre" <[hidden email]> wrote:

>First of all, thank you for all the quick replies.  Here is a solution
>that
>worked for me.
>
>mylist2 <- lapply(mylist, function(e){colnames(e) <- paste0('pop',1:12);
>return(e)})
>
>> head(mylist2[[1]])
>        pop1  pop2  pop3  pop4  pop5  pop6  pop7  pop8  pop9 pop10 pop11
>pop12
>L0001.1 0.60 0.500 0.325 0.675 0.600 0.500 0.500 0.375 0.550 0.475 0.350
>0.275
>L0001.2 0.40 0.500 0.675 0.325 0.400 0.500 0.500 0.625 0.450 0.525 0.650
>0.725
>
>While we are at this, I wanted to create a 13th column in each data frame
>for average of each row.
>
># Calculate average
>myavg <- lapply(mylist2, function(e) rowSums(mylist2)/12)
>
># Attach to the main data frame
>mylist3 <- cbind(mylist2, myavg)
>
>This does not work the way I imagined it would.  The myavg vector is
>attached directly to mylist2, not to individual dataframes within.
>
>p.s. Is it a standard convention to always copy the reply to the last
>person who responded?
>
>On Mon, Mar 30, 2015 at 10:56 AM, Sven E. Templer <[hidden email]>
>wrote:
>
>> On 30 March 2015 at 16:47, Sarah Goslee <[hidden email]> wrote:
>>
>> > colnames(e) <- paste0('pop',1:12)
>> >
>> > isn't a function and doesn't return anything.
>> >
>>
>> But
>> function(e){colnames(e) <- paste0('pop', 1:2)}
>> is a function and it returns something (the last evaluated expression! -
>> here the paste0 return):
>>
>> > mylist2 <- lapply(mylist, function(e){colnames(e) <- paste0('pop',
>>1:2)})
>> > mylist2
>> [[1]]
>> [1] "pop1" "pop2"
>>
>> [[2]]
>> [1] "pop1" "pop2"
>>
>> [[3]]
>> [1] "pop1" "pop2"
>>
>>  from ?return:
>>
>> If the end of a function is reached without calling return, the value of
>> the last evaluated expression is returned.
>>
>> >
>> > > mylist <- list(
>> > + data.frame(a = runif(10), b = runif(10)),
>> > + data.frame(c = runif(10), d = runif(10)),
>> > + data.frame(e = runif(10), f = runif(10)))
>> > > mylist2 <- lapply(mylist, function(e){colnames(e) <- paste0('pop',
>> 1:2);
>> > e})
>> > > colnames(mylist2[[1]])
>> > [1] "pop1" "pop2"
>> >
>> > Sarah
>> >
>> > On Mon, Mar 30, 2015 at 9:54 AM, Vikram Chhatre
>> > <[hidden email]> wrote:
>> > >> summary(mygenfreqt)
>> > >                   Length Class  Mode
>> > > dat1.str 59220  -none- numeric
>> > > dat2.str 59220  -none- numeric
>> > > dat3.str 59220  -none- numeric
>> > >
>> > >> head(mylist[[1]])
>> > >            1     2     3     4     5     6     7     8     9    10
>> 11
>> > >  12
>> > > L0001.1 0.60 0.500 0.325 0.675 0.600 0.500 0.500 0.375 0.550 0.475
>> 0.350
>> > > 0.275
>> > > L0001.2 0.40 0.500 0.675 0.325 0.400 0.500 0.500 0.625 0.450 0.525
>> 0.650
>> > > 0.725
>> > >
>> > > I want to change 1:12 to pop1:pop12
>> > >
>> > > mylist<- lapply(mylist, function(e) colnames(e) <-
>>paste0('pop',1:12))
>> > >
>> > > What this is doing is replacing the data frames with just names
>> > > pop1:pop12.  I just want to replace the column labels.
>> > >
>> > > Thanks for any suggestions.
>> > >
>> >
>> > --
>> > Sarah Goslee
>> > http://www.functionaldiversity.org
>> >
>> > ______________________________________________
>> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> > 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.
>> >
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>>
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>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 -- To UNSUBSCRIBE and more, see
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: changing column labels for data frames inside a list

r-help-2
In reply to this post by Vikram Chhatre
> Date: Mon, 30 Mar 2015 09:54:39 -0400
> From: Vikram Chhatre <[hidden email]>
> To: [hidden email]
> Subject: [R] changing column labels for data frames inside a list
> Message-ID:
>         <CAJZnH0=uGay_1VzjVTMMc=[hidden email]
> Content-Type: text/plain; charset="UTF-8"
>
> > summary(mygenfreqt)
>                   Length Class  Mode
> dat1.str 59220  -none- numeric
> dat2.str 59220  -none- numeric
> dat3.str 59220  -none- numeric
>
> > head(mylist[[1]])
>            1     2     3     4     5     6     7     8     9    10
>  12
> L0001.1 0.60 0.500 0.325 0.675 0.600 0.500 0.500 0.375 0.550 0.475 0.3
> 0.275
> L0001.2 0.40 0.500 0.675 0.325 0.400 0.500 0.500 0.625 0.450 0.525 0.6
> 0.725
>
> I want to change 1:12 to pop1:pop12
>
> mylist<- lapply(mylist, function(e) colnames(e) <- paste0('pop',1:12))
>
> What this is doing is replacing the data frames with just names
> pop1:pop12.  I just want to replace the column labels.
>
> Thanks for any suggestions.

Some readers have already replied, but here is another option that exploits lapply()'s "..." parameter.  First, we make a reproducible example.

(lista <- list(mtcars, mtcars))

Now, we get the unique number of columns of the data frames in the variable "lista".

(n.cols <- unique(sapply(lista, ncol)))

Finally, we call lapply() and `colnames<-` to change the column names of both data frames in "lista".  See lapply()'s "..." parameter (?lapply).

(lista <- lapply(X = lista, FUN = `colnames<-`, paste0("pop", seq_len(n.cols))))

>         [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.