On Thursday 21 February 2008 (19:22:40), Lauri Nikkinen wrote:
> R users,
>
> I have a simple lapply question.
>
> g <- list(a=1:3, b=4:6, c=7:9)
> g <- lapply(g, function(x) as.data.frame(x))

> And I would like to have
>
> $a
>   x var1
> 1 1    a
> 2 2    a
> 3 3    a
>
> $b
>   x var1
> 1 4    b
> 2 5    b
> 3 6    b
>
> $c
>   x var1
> 1 7    c
> 2 8    c
> 3 9    c

The following seems to work:

sapply(names(g), 
        function(n) 
                cbind(
                        g[[n]], 
                        var1 = rep(n,each=nrow(g[[n]]))
                ),
                simplify=FALSE)



Best,
Martin

______________________________________________
R-help@r-project.org mailing list
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 via email to