I'm hoping this is an easy problem that I'm missing something obvious.
Given:
x=c(1,1,1,2,2,3,3,3)
y=c(1:length(x))
dataframe=data.frame(x,y)
I would like to convert this to a list for use with certain functions,
where each entry of the list is a subsetted dataframe based on
dataframe$x
I can do this "brute force" by a for-next loop:
unique_x=unique(dataframe$x)
unique_x_N=length(unique_x)
dataframe_to_list=vector(mode="list",length=unique_x_N)
for(i in 1:unique_x_N)
{
dataframe_to_list[[i]]=subset(dataframe,x==unique_x[i])
}
My R-gut is telling me there's a much more efficient way of doing this
-- is it right?
--j
?split -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Jonathan Greenberg > Sent: Thursday, July 28, 2011 2:14 PM > To: r-help > Subject: [R] Data frame to list > > I'm hoping this is an easy problem that I'm missing something obvious. > Given: > > x=c(1,1,1,2,2,3,3,3) > y=c(1:length(x)) > dataframe=data.frame(x,y) > > I would like to convert this to a list for use with certain functions, > where each entry of the list is a subsetted dataframe based on > dataframe$x > > I can do this "brute force" by a for-next loop: > > unique_x=unique(dataframe$x) > unique_x_N=length(unique_x) > dataframe_to_list=vector(mode="list",length=unique_x_N) > for(i in 1:unique_x_N) > { > dataframe_to_list[[i]]=subset(dataframe,x==unique_x[i]) > > } > > My R-gut is telling me there's a much more efficient way of doing this > -- is it right? > > --j > > ______________________________________________ > 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.
Try this:
split(dataframe, dataframe$x)
Jean
`·.,, ><(((º> `·.,, ><(((º> `·.,, ><(((º>
Jean V. Adams
Statistician
U.S. Geological Survey
Great Lakes Science Center
223 East Steinfest Road
Antigo, WI 54409 USA
From:
Jonathan Greenberg <jgrn@illinois.edu>
To:
r-help <r-help@r-project.org>
Date:
07/28/2011 03:22 PM
Subject:
[R] Data frame to list
Sent by:
r-help-bounces@r-project.org
I'm hoping this is an easy problem that I'm missing something obvious.
Given:
x=c(1,1,1,2,2,3,3,3)
y=c(1:length(x))
dataframe=data.frame(x,y)
I would like to convert this to a list for use with certain functions,
where each entry of the list is a subsetted dataframe based on
dataframe$x
I can do this "brute force" by a for-next loop:
unique_x=unique(dataframe$x)
unique_x_N=length(unique_x)
dataframe_to_list=vector(mode="list",length=unique_x_N)
for(i in 1:unique_x_N)
{
dataframe_to_list[[i]]=subset(dataframe,x==unique_x[i])
}
My R-gut is telling me there's a much more efficient way of doing this
-- is it right?
--j
______________________________________________
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.
[[alternative HTML version deleted]]
On Jul 28, 2011, at 4:14 PM, Jonathan Greenberg wrote:> I'm hoping this is an easy problem that I'm missing something > obvious. Given: > > x=c(1,1,1,2,2,3,3,3) > y=c(1:length(x)) > dataframe=data.frame(x,y) > > I would like to convert this to a list for use with certain functions, > where each entry of the list is a subsetted dataframe based on > dataframe$x?split > split(dataframe, x) $`1` x y 1 1 1 2 1 2 3 1 3 $`2` x y 4 2 4 5 2 5 $`3` x y 6 3 6 7 3 7 8 3 8> > I can do this "brute force" by a for-next loop: > > unique_x=unique(dataframe$x) > unique_x_N=length(unique_x) > dataframe_to_list=vector(mode="list",length=unique_x_N) > for(i in 1:unique_x_N) > { > dataframe_to_list[[i]]=subset(dataframe,x==unique_x[i]) > > } > > My R-gut is telling me there's a much more efficient way of doing this > -- is it right? > > --j > > ______________________________________________ > 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