Vikram Chhatre
2015-Mar-30 13:54 UTC
[R] changing column labels for data frames inside a list
> 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]]
Sarah Goslee
2015-Mar-30 14:47 UTC
[R] changing column labels for data frames inside a list
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
<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
MacQueen, Don
2015-Mar-30 14:48 UTC
[R] changing column labels for data frames inside a list
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" <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.
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org 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.
Ivan Calandra
2015-Mar-30 14:50 UTC
[R] changing column labels for data frames inside a list
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 ivan.calandra at univ-reims.fr 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]] > > ______________________________________________ > R-help at r-project.org 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. >
Sven E. Templer
2015-Mar-30 14:56 UTC
[R] changing column labels for data frames inside a list
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 > > ______________________________________________ > R-help at r-project.org 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]]