Re: [R] How to get names of a list into df:s?

2008-02-21 Thread Bert Gunter
Jim's solution showed me that mine should be simplified to:

mapply(data.frame,value=g,nm=names(g),SIMPLIFY=FALSE)

This has the slight advantage of automatically naming the list.

Cheers,
Bert Gunter

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of jim holtman
Sent: Thursday, February 21, 2008 4:53 PM
To: Lauri Nikkinen
Cc: [EMAIL PROTECTED]
Subject: Re: [R] How to get names of a list into df:s?

Here is one way of doing it:

> lapply(names(g), function(z)cbind(x=g[[z]], var1=z))
[[1]]
  x var1
1 1a
2 2a
3 3a

[[2]]
  x var1
1 4b
2 5b
3 6b

[[3]]
  x var1
1 7c
2 8c
3 9c


On Thu, Feb 21, 2008 at 1:22 PM, Lauri Nikkinen <[EMAIL PROTECTED]>
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))
> lapply(g, function(x) cbind(x, var1 = rep(names(g),
each=nrow(x))[1:nrow(x)]))
>
> I get
>
> $a
>  x var1
> 1 1a
> 2 2a
> 3 3a
>
> $b
>  x var1
> 1 4a
> 2 5a
> 3 6a
>
> $c
>  x var1
> 1 7a
> 2 8a
> 3 9a
>
> And I would like to have
>
> $a
>  x var1
> 1 1a
> 2 2a
> 3 3a
>
> $b
>  x var1
> 1 4b
> 2 5b
> 3 6b
>
> $c
>  x var1
> 1 7c
> 2 8c
> 3 9c
>
> How should I modify my lapply clause to achieve this?
>
> Best regards,
> Lauri
>
> > sessionInfo()
> R version 2.6.1 (2007-11-26)
> i386-apple-darwin8.10.1
>
> locale:
> C
>
> attached base packages:
> [1] stats graphics  grDevices utils datasets  methods   base
>
> __
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

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

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


Re: [R] How to get names of a list into df:s?

2008-02-21 Thread jim holtman
Here is one way of doing it:

> lapply(names(g), function(z)cbind(x=g[[z]], var1=z))
[[1]]
  x var1
1 1a
2 2a
3 3a

[[2]]
  x var1
1 4b
2 5b
3 6b

[[3]]
  x var1
1 7c
2 8c
3 9c


On Thu, Feb 21, 2008 at 1:22 PM, Lauri Nikkinen <[EMAIL PROTECTED]> 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))
> lapply(g, function(x) cbind(x, var1 = rep(names(g), each=nrow(x))[1:nrow(x)]))
>
> I get
>
> $a
>  x var1
> 1 1a
> 2 2a
> 3 3a
>
> $b
>  x var1
> 1 4a
> 2 5a
> 3 6a
>
> $c
>  x var1
> 1 7a
> 2 8a
> 3 9a
>
> And I would like to have
>
> $a
>  x var1
> 1 1a
> 2 2a
> 3 3a
>
> $b
>  x var1
> 1 4b
> 2 5b
> 3 6b
>
> $c
>  x var1
> 1 7c
> 2 8c
> 3 9c
>
> How should I modify my lapply clause to achieve this?
>
> Best regards,
> Lauri
>
> > sessionInfo()
> R version 2.6.1 (2007-11-26)
> i386-apple-darwin8.10.1
>
> locale:
> C
>
> attached base packages:
> [1] stats graphics  grDevices utils datasets  methods   base
>
> __
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

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


Re: [R] How to get names of a list into df:s?

2008-02-21 Thread Bert Gunter
mapply(data.frame,value=g,nm=lapply(names(g),rep,length(g)),SIMPLIFY=FALSE)

Cheers,

Bert Gunter
Genentech

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of John Kane
Sent: Thursday, February 21, 2008 2:05 PM
To: Lauri Nikkinen; [EMAIL PROTECTED]
Subject: Re: [R] How to get names of a list into df:s?

I don't see any difference 
--- Lauri Nikkinen <[EMAIL PROTECTED]> 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))
> lapply(g, function(x) cbind(x, var1 = rep(names(g),
> each=nrow(x))[1:nrow(x)]))
> 
> I get
> 
> $a
>   x var1
> 1 1a
> 2 2a
> 3 3a
> 
> $b
>   x var1
> 1 4a
> 2 5a
> 3 6a
> 
> $c
>   x var1
> 1 7a
> 2 8a
> 3 9a
> 
> And I would like to have
> 
> $a
>   x var1
> 1 1a
> 2 2a
> 3 3a
> 
> $b
>   x var1
> 1 4b
> 2 5b
> 3 6b
> 
> $c
>   x var1
> 1 7c
> 2 8c
> 3 9c
> 
> How should I modify my lapply clause to achieve
> this?
> 
> Best regards,
> Lauri
> 
> > sessionInfo()
> R version 2.6.1 (2007-11-26)
> i386-apple-darwin8.10.1
> 
> locale:
> C
> 
> attached base packages:
> [1] stats graphics  grDevices utils datasets
>  methods   base
> 
> __
> 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.
>

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

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


Re: [R] How to get names of a list into df:s?

2008-02-21 Thread John Kane
I don't see any difference 
--- Lauri Nikkinen <[EMAIL PROTECTED]> 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))
> lapply(g, function(x) cbind(x, var1 = rep(names(g),
> each=nrow(x))[1:nrow(x)]))
> 
> I get
> 
> $a
>   x var1
> 1 1a
> 2 2a
> 3 3a
> 
> $b
>   x var1
> 1 4a
> 2 5a
> 3 6a
> 
> $c
>   x var1
> 1 7a
> 2 8a
> 3 9a
> 
> And I would like to have
> 
> $a
>   x var1
> 1 1a
> 2 2a
> 3 3a
> 
> $b
>   x var1
> 1 4b
> 2 5b
> 3 6b
> 
> $c
>   x var1
> 1 7c
> 2 8c
> 3 9c
> 
> How should I modify my lapply clause to achieve
> this?
> 
> Best regards,
> Lauri
> 
> > sessionInfo()
> R version 2.6.1 (2007-11-26)
> i386-apple-darwin8.10.1
> 
> locale:
> C
> 
> attached base packages:
> [1] stats graphics  grDevices utils datasets
>  methods   base
> 
> __
> 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.
>

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


Re: [R] How to get names of a list into df:s?

2008-02-21 Thread Martin Elff
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.


Re: [R] How to get names of a list into df:s?

2008-02-21 Thread Henrique Dallazuanna
Try this:

g <- list(a=1:3, b=4:6, c=7:9)
with(stack(g), split(stack(g), ind))

On 21/02/2008, Lauri Nikkinen <[EMAIL PROTECTED]> 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))
>  lapply(g, function(x) cbind(x, var1 = rep(names(g), 
> each=nrow(x))[1:nrow(x)]))
>
>  I get
>
>  $a
>   x var1
>  1 1a
>  2 2a
>  3 3a
>
>  $b
>   x var1
>  1 4a
>  2 5a
>  3 6a
>
>  $c
>   x var1
>  1 7a
>  2 8a
>  3 9a
>
>  And I would like to have
>
>  $a
>   x var1
>  1 1a
>  2 2a
>  3 3a
>
>  $b
>   x var1
>  1 4b
>  2 5b
>  3 6b
>
>  $c
>   x var1
>  1 7c
>  2 8c
>  3 9c
>
>  How should I modify my lapply clause to achieve this?
>
>  Best regards,
>  Lauri
>
>  > sessionInfo()
>  R version 2.6.1 (2007-11-26)
>  i386-apple-darwin8.10.1
>
>  locale:
>  C
>
>  attached base packages:
>  [1] stats graphics  grDevices utils datasets  methods   base
>
>  __
>  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.
>


-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

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


[R] How to get names of a list into df:s?

2008-02-21 Thread Lauri Nikkinen
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))
lapply(g, function(x) cbind(x, var1 = rep(names(g), each=nrow(x))[1:nrow(x)]))

I get

$a
  x var1
1 1a
2 2a
3 3a

$b
  x var1
1 4a
2 5a
3 6a

$c
  x var1
1 7a
2 8a
3 9a

And I would like to have

$a
  x var1
1 1a
2 2a
3 3a

$b
  x var1
1 4b
2 5b
3 6b

$c
  x var1
1 7c
2 8c
3 9c

How should I modify my lapply clause to achieve this?

Best regards,
Lauri

> sessionInfo()
R version 2.6.1 (2007-11-26)
i386-apple-darwin8.10.1

locale:
C

attached base packages:
[1] stats graphics  grDevices utils datasets  methods   base

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