[R] Help

Sarah Goslee sarah.goslee at gmail.com
Fri Nov 18 20:31:01 CET 2016


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
>
>



More information about the R-help mailing list