Dear, I have the following list (mylist), in which I need to pass to the column 2 the name of the list itself. So, running the follwing commands: A= data.frame(1:2,3) B= data.frame(1:4,3) C= data.frame(c(1:2),c(4:5)) mylist=list(A=A,B=A,C=C) lapply(mylist, setNames, paste(c("CaZyme"))) The output would be:> lapply(mylist, setNames, paste(c("CaZyme")))$A CaZyme NA 1 1 3 2 2 3 $B CaZyme NA 1 1 3 2 2 3 $C CaZyme NA 1 1 4 2 2 5 3 3 6 My question is: How could I name the second column with the name of each dataframe of the list, such that NA would be substitute for A, B and C, respectively. Thank you very much, Andre -- Andre [[alternative HTML version deleted]]
Thanks for the useful reproducible example. Here's one of the various ways this can be done:> lapply(seq_along(mylist), function(i)setNames(mylist[[i]], c("CaZyme", names(mylist)[i])))[[1]] CaZyme A 1 1 3 2 2 3 [[2]] CaZyme B 1 1 3 2 2 3 [[3]] CaZyme C 1 1 4 2 2 5 On Fri, Nov 18, 2016 at 2:02 PM, Andr? Luis Neves <andrluis at ualberta.ca> wrote:> Dear, > > I have the following list (mylist), in which I need to pass to the column 2 > the name of the list itself. > So, running the follwing commands: > > A= data.frame(1:2,3) > B= data.frame(1:4,3) > C= data.frame(c(1:2),c(4:5)) > mylist=list(A=A,B=A,C=C) > lapply(mylist, setNames, paste(c("CaZyme"))) > > The output would be: >> lapply(mylist, setNames, paste(c("CaZyme"))) > $A > CaZyme NA > 1 1 3 > 2 2 3 > > $B > CaZyme NA > 1 1 3 > 2 2 3 > > $C > CaZyme NA > 1 1 4 > 2 2 5 > 3 3 6 > > My question is: > How could I name the second column with the name of each dataframe of the > list, such that NA would be substitute for A, B and C, respectively. > > Thank you very much, > > Andre > >
Thank you very much, Sarah! It worked great in my dataset! Andre On Fri, Nov 18, 2016 at 12:31 PM, Sarah Goslee <sarah.goslee at gmail.com> wrote:> Thanks for the useful reproducible example. > > Here's one of the various ways this can be done: > > > lapply(seq_along(mylist), function(i)setNames(mylist[[i]], c("CaZyme", > names(mylist)[i]))) > [[1]] > CaZyme A > 1 1 3 > 2 2 3 > > [[2]] > CaZyme B > 1 1 3 > 2 2 3 > > [[3]] > CaZyme C > 1 1 4 > 2 2 5 > > On Fri, Nov 18, 2016 at 2:02 PM, Andr? Luis Neves <andrluis at ualberta.ca> > wrote: > > Dear, > > > > I have the following list (mylist), in which I need to pass to the > column 2 > > the name of the list itself. > > So, running the follwing commands: > > > > A= data.frame(1:2,3) > > B= data.frame(1:4,3) > > C= data.frame(c(1:2),c(4:5)) > > mylist=list(A=A,B=A,C=C) > > lapply(mylist, setNames, paste(c("CaZyme"))) > > > > The output would be: > >> lapply(mylist, setNames, paste(c("CaZyme"))) > > $A > > CaZyme NA > > 1 1 3 > > 2 2 3 > > > > $B > > CaZyme NA > > 1 1 3 > > 2 2 3 > > > > $C > > CaZyme NA > > 1 1 4 > > 2 2 5 > > 3 3 6 > > > > My question is: > > How could I name the second column with the name of each dataframe of the > > list, such that NA would be substitute for A, B and C, respectively. > > > > Thank you very much, > > > > Andre > > > > >-- Andre [[alternative HTML version deleted]]