Hi all, this is a reoccurring theme in my programming and I need some help with it. When I use a built in function and need to use it on a subset of my data frame, I always end up using the subset function first, but this seems very clunky. For example, if I have years 2003:2013 with season "a" and "b" within each year, and I want to create a smooth.spline, I end up creating a subset for each year and season, and then have a smooth spline function for each year and season. Can I do this more efficiently? The subsets are below: size.2003<-subset(size,Year==2003&Season=="a") size.2004<-subset(size,Year==2004&Season=="a") size.2005<-subset(size,Year==2005&Season=="a") size.2006<-subset(size,Year==2006&Season=="a") size.2007<-subset(size,Year==2007&Season=="a") size.2008<-subset(size,Year==2008&Season=="a") size.2009<-subset(size,Year==2009&Season=="a") size.2010<-subset(size,Year==2010&Season=="a") size.2011<-subset(size,Year==2011&Season=="a") size.2012<-subset(size,Year==2012&Season=="a") size.2013<-subset(size,Year==2013&Season=="a") size.2003b<-subset(size,Year==2003&Season=="b") size.2004b<-subset(size,Year==2004&Season=="b") size.2005b<-subset(size,Year==2005&Season=="b") size.2006b<-subset(size,Year==2006&Season=="b") size.2007b<-subset(size,Year==2007&Season=="b") size.2008b<-subset(size,Year==2008&Season=="b") size.2009b<-subset(size,Year==2009&Season=="b") size.2010b<-subset(size,Year==2010&Season=="b") size.2011b<-subset(size,Year==2011&Season=="b") size.2012b<-subset(size,Year==2012&Season=="b") size.2013b<-subset(size,Year==2013&Season=="b") The smooth.spline is below 2003<-with(size.2003,smooth.spline(Size,Prop,spar=0.25)) 2004<-with(size.2004,smooth.spline(Size,Prop,spar=0.25)) 2005<-with(size.2005,smooth.spline(Size,Prop,spar=0.25)) etc. etc. M. Keith Cox, Ph.D. Principal MKConsulting 17105 Glacier Hwy Juneau, AK 99801 U.S. 907.957.4606 [[alternative HTML version deleted]]
Have you read "An Introduction to R" and sections on indexing (?"[") where this is discussed. Have you read about apply type functions there like ?tapply. If not, don't you think you should. If so, read again. Cheers, Bert Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." H. Gilbert Welch On Mon, May 19, 2014 at 5:34 PM, Marlin Keith Cox <marlinkcox at gmail.com> wrote:> Hi all, this is a reoccurring theme in my programming and I need some help > with it. When I use a built in function and need to use it on a subset of > my data frame, I always end up using the subset function first, but this > seems very clunky. For example, if I have years 2003:2013 with season "a" > and "b" within each year, and I want to create a smooth.spline, I end up > creating a subset for each year and season, and then have a smooth spline > function for each year and season. Can I do this more efficiently? > > > The subsets are below: > > > size.2003<-subset(size,Year==2003&Season=="a") > > size.2004<-subset(size,Year==2004&Season=="a") > > size.2005<-subset(size,Year==2005&Season=="a") > > size.2006<-subset(size,Year==2006&Season=="a") > > size.2007<-subset(size,Year==2007&Season=="a") > > size.2008<-subset(size,Year==2008&Season=="a") > > size.2009<-subset(size,Year==2009&Season=="a") > > size.2010<-subset(size,Year==2010&Season=="a") > > size.2011<-subset(size,Year==2011&Season=="a") > > size.2012<-subset(size,Year==2012&Season=="a") > > size.2013<-subset(size,Year==2013&Season=="a") > > size.2003b<-subset(size,Year==2003&Season=="b") > > size.2004b<-subset(size,Year==2004&Season=="b") > > size.2005b<-subset(size,Year==2005&Season=="b") > > size.2006b<-subset(size,Year==2006&Season=="b") > > size.2007b<-subset(size,Year==2007&Season=="b") > > size.2008b<-subset(size,Year==2008&Season=="b") > > size.2009b<-subset(size,Year==2009&Season=="b") > > size.2010b<-subset(size,Year==2010&Season=="b") > > size.2011b<-subset(size,Year==2011&Season=="b") > > size.2012b<-subset(size,Year==2012&Season=="b") > > size.2013b<-subset(size,Year==2013&Season=="b") > > The smooth.spline is below > > 2003<-with(size.2003,smooth.spline(Size,Prop,spar=0.25)) > > 2004<-with(size.2004,smooth.spline(Size,Prop,spar=0.25)) > 2005<-with(size.2005,smooth.spline(Size,Prop,spar=0.25)) > etc. etc. > > M. Keith Cox, Ph.D. > Principal > MKConsulting > 17105 Glacier Hwy > Juneau, AK 99801 > U.S. 907.957.4606 > > [[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.
It would have been nice if you at least supplied a subset of the data, but here is a try at it: myList <- split(size, list(size$Year, size$Season)) result <- lapply(myList, function(.sub){ smooth.spline(.sub$Size, spar = 0.25) }) Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. On Mon, May 19, 2014 at 8:34 PM, Marlin Keith Cox <marlinkcox@gmail.com>wrote:> Hi all, this is a reoccurring theme in my programming and I need some help > with it. When I use a built in function and need to use it on a subset of > my data frame, I always end up using the subset function first, but this > seems very clunky. For example, if I have years 2003:2013 with season "a" > and "b" within each year, and I want to create a smooth.spline, I end up > creating a subset for each year and season, and then have a smooth spline > function for each year and season. Can I do this more efficiently? > > > The subsets are below: > > > size.2003<-subset(size,Year==2003&Season=="a") > > size.2004<-subset(size,Year==2004&Season=="a") > > size.2005<-subset(size,Year==2005&Season=="a") > > size.2006<-subset(size,Year==2006&Season=="a") > > size.2007<-subset(size,Year==2007&Season=="a") > > size.2008<-subset(size,Year==2008&Season=="a") > > size.2009<-subset(size,Year==2009&Season=="a") > > size.2010<-subset(size,Year==2010&Season=="a") > > size.2011<-subset(size,Year==2011&Season=="a") > > size.2012<-subset(size,Year==2012&Season=="a") > > size.2013<-subset(size,Year==2013&Season=="a") > > size.2003b<-subset(size,Year==2003&Season=="b") > > size.2004b<-subset(size,Year==2004&Season=="b") > > size.2005b<-subset(size,Year==2005&Season=="b") > > size.2006b<-subset(size,Year==2006&Season=="b") > > size.2007b<-subset(size,Year==2007&Season=="b") > > size.2008b<-subset(size,Year==2008&Season=="b") > > size.2009b<-subset(size,Year==2009&Season=="b") > > size.2010b<-subset(size,Year==2010&Season=="b") > > size.2011b<-subset(size,Year==2011&Season=="b") > > size.2012b<-subset(size,Year==2012&Season=="b") > > size.2013b<-subset(size,Year==2013&Season=="b") > > The smooth.spline is below > > 2003<-with(size.2003,smooth.spline(Size,Prop,spar=0.25)) > > 2004<-with(size.2004,smooth.spline(Size,Prop,spar=0.25)) > 2005<-with(size.2005,smooth.spline(Size,Prop,spar=0.25)) > etc. etc. > > M. Keith Cox, Ph.D. > Principal > MKConsulting > 17105 Glacier Hwy > Juneau, AK 99801 > U.S. 907.957.4606 > > [[alternative HTML version deleted]] > > ______________________________________________ > 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]]