[R] Subsets of a function

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Tue May 20 09:35:01 CEST 2014


Another option is the plyr package.

library(plyr)
result <- dlply(size, ~ Year +Season, function(.sub){
        with(.sub, smooth.spline(Size, Prop, spar = 0.25))
}


ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium
+ 32 2 525 02 51
+ 32 54 43 61 85
Thierry.Onkelinx op inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey

-----Oorspronkelijk bericht-----
Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org] Namens jim holtman
Verzonden: dinsdag 20 mei 2014 2:44
Aan: Marlin Keith Cox
CC: r-help op r-project.org
Onderwerp: Re: [R] Subsets of a function

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 op 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 op 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]]

______________________________________________
R-help op 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.
* * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * *
Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document.
The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.



More information about the R-help mailing list