[R] From data frame to list object

Bogaso Christofer bogaso.christofer at gmail.com
Mon Jan 31 19:56:57 CET 2011


Thanks David for this reply. However if my data frame has only 2 columns
then it is working fine. It is not working for a general setting:

dfrm <- data.frame(x=rnorm(18), y=rep(c("a", "b", "c"), each=6),
z=rep(c("x", "y", "z"), each=2))
tapply(dfrm[,1], dfrm$y, c) # this is working fine

> tapply(dfrm[,c(1,3)], dfrm$y, c)  # this is giving error!
Error in tapply(dfrm[, c(1, 3)], dfrm$y, c) : 
  arguments must have same length

Can you please help me how to modify that?

Thanks,

-----Original Message-----
From: David Winsemius [mailto:dwinsemius at comcast.net] 
Sent: 31 January 2011 23:26
To: Bogaso Christofer
Cc: r-help at r-project.org
Subject: Re: [R] From data frame to list object


On Jan 31, 2011, at 1:03 PM, Bogaso Christofer wrote:

> Dear all, let say I have following data frame:
>
>

 > dfrm <- data.frame(x=rnorm(18), y=rep(c("a", "b", "c"), each=6))  >
tapply(dfrm$x, dfrm$y, c) $a [1]  0.9711995  1.4018345 -1.4355713 -0.5106138
-0.8470171 [6]  1.1634586

$b
[1] -0.8058164  0.4977112  1.1556391  0.8158588  0.2549273 [6]  3.0758260

$c
[1]  0.437345128 -0.415874363  0.003230285 -0.737117910 [5]  1.247972964
0.903001077


>
>> data.frame(x=rnorm(18), y=rep(c("a", "b", "c"), each=6))
>
>              x y
>
> 1  -1.072152537 a
>
> 2   0.382985265 a
>
> 3   0.058877377 a
>
> 4  -0.006911939 a
>
> 5  -2.355269051 a
>
> 6  -0.303095553 a
>
> 7   0.484038422 b
>
> 8   0.733928931 b
>
> 9  -1.136014346 b
>
> 10  0.503552090 b
>
> 11  1.708609658 b
>
> 12 -0.294599403 b
>
> 13  1.239308497 c
>
> 14  0.754081946 c
>
> 15 -0.237346858 c
>
> 16 -0.051011439 c
>
> 17 -0.618675146 c
>
> 18  0.537612359 c
>
>
>
>> From this data frame I want to create a "list" of length 3, where 
>> each
> element of this list will be a vector corresponding to the value of y. 
> For example, 1st element will be all "x" values corresponding to the 
> "y=a", and similarly the other elements of this list. Can somebody 
> point me how to do this without having some "for" loop?
>
>
>
> Thanks and regards,
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list