[R] changing column labels for data frames inside a list

Sven E. Templer sven.templer at gmail.com
Mon Mar 30 18:12:41 CEST 2015


On 30 March 2015 at 17:50, Sarah Goslee <sarah.goslee at gmail.com> wrote:

> On Mon, Mar 30, 2015 at 11:43 AM, Sven E. Templer
> <sven.templer at gmail.com> wrote:
> >
> >
> > On 30 March 2015 at 17:31, Bert Gunter <gunter.berton at gene.com> 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 <
> sven.templer at gmail.com>
> >> wrote:
> >> > On 30 March 2015 at 16:47, Sarah Goslee <sarah.goslee at gmail.com>
> 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
> >> >> <crypticlineage at gmail.com> 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]]



More information about the R-help mailing list