Hi All? I have a data frame with over 13000 rows and 4 columns. A mini data frame is given at the bottom. I want to split the data frame into lists each corresponds to single year which ranges from 1990 to 2018). I wanted to use the split function, but it requires a vector of the same length as MyDate which contains many multiples of each year. Any help is highly appreciated. I want the following results: List 1990 MyDate MyNo MyDes 1990 1990 1990 ?... List 2000 2000 2000 2000 ?... List 2001 2001 2001 2001 2001 ?... List 2018 2018 2018 2018 ?... Sample data frame mydf <- data.frame(MyDate=c("1990-01-01","1990-04-07","2000-04-05","2018-01-04"),MyNo=c(1,2,3,4),MyDes=c("AA","BB","CC","DD")) EK
Couldn't you just use the substr function to pull the year out yourself to make the grouping column? On March 15, 2019 10:40:01 PM PDT, Ek Esawi <esawiek at gmail.com> wrote:>Hi All? > >I have a data frame with over 13000 rows and 4 columns. A mini data >frame is given at the bottom. I want to split the data frame into >lists each corresponds to single year which ranges from 1990 to 2018). >I wanted to use the split function, but it requires a vector of the >same length as MyDate which contains many multiples of each year. >Any help is highly appreciated. > >I want the following results: >List 1990 >MyDate MyNo MyDes >1990 >1990 >1990 >?... >List 2000 >2000 >2000 >2000 >?... >List 2001 >2001 >2001 >2001 >2001 >?... >List 2018 >2018 >2018 >2018 >?... > >Sample data frame > >mydf <- >data.frame(MyDate=c("1990-01-01","1990-04-07","2000-04-05","2018-01-04"),MyNo=c(1,2,3,4),MyDes=c("AA","BB","CC","DD")) > > >EK > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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.-- Sent from my phone. Please excuse my brevity.
Rainer Schuermann
2019-Mar-16 06:05 UTC
[R] Split a DF on Date column for each single year
In your sample data.frame, MyDate and MyDes are factors; is that what you want? rs On Samstag, 16. M?rz 2019 01:40:01 CET Ek Esawi wrote:> Hi All? > > I have a data frame with over 13000 rows and 4 columns. A mini data > frame is given at the bottom. I want to split the data frame into > lists each corresponds to single year which ranges from 1990 to 2018). > I wanted to use the split function, but it requires a vector of the > same length as MyDate which contains many multiples of each year. > Any help is highly appreciated. > > I want the following results: > List 1990 > MyDate MyNo MyDes > 1990 > 1990 > 1990 > ?... > List 2000 > 2000 > 2000 > 2000 > ?... > List 2001 > 2001 > 2001 > 2001 > 2001 > ?... > List 2018 > 2018 > 2018 > 2018 > ?... > > Sample data frame > > mydf <- data.frame(MyDate=c("1990-01-01","1990-04-07","2000-04-05","2018-01-04"),MyNo=c(1,2,3,4),MyDes=c("AA","BB","CC","DD")) > > > EK > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >
Thank you Jeff and Rainer. I will try Jeff's idea using the sub string. function to extract the year and split on that. Thanks again to both--EK On Sat, Mar 16, 2019 at 1:52 AM Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote:> > Couldn't you just use the substr function to pull the year out yourself to make the grouping column? > > On March 15, 2019 10:40:01 PM PDT, Ek Esawi <esawiek at gmail.com> wrote: > >Hi All? > > > >I have a data frame with over 13000 rows and 4 columns. A mini data > >frame is given at the bottom. I want to split the data frame into > >lists each corresponds to single year which ranges from 1990 to 2018). > >I wanted to use the split function, but it requires a vector of the > >same length as MyDate which contains many multiples of each year. > >Any help is highly appreciated. > > > >I want the following results: > >List 1990 > >MyDate MyNo MyDes > >1990 > >1990 > >1990 > >?... > >List 2000 > >2000 > >2000 > >2000 > >?... > >List 2001 > >2001 > >2001 > >2001 > >2001 > >?... > >List 2018 > >2018 > >2018 > >2018 > >?... > > > >Sample data frame > > > >mydf <- > >data.frame(MyDate=c("1990-01-01","1990-04-07","2000-04-05","2018-01-04"),MyNo=c(1,2,3,4),MyDes=c("AA","BB","CC","DD")) > > > > > >EK > > > >______________________________________________ > >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > >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. > > -- > Sent from my phone. Please excuse my brevity.