Hello R-help, I am trying to compute the mean of a quarterback's career fantasy football stats, but I wish to exclude his 2014 stats from the mean, as that will be the test data for the model I am trying to build for my academic undergrad research. The code for figuring out the mean of his Yds for every career Game 1 was simple: *mean(brady.t$Yds[brady.t$G. == 1])* How can I make an "if-then" statement though so that his 2014 stats are excluded? Or is there an easier way besides "if-then?" Thank you, AKJ [[alternative HTML version deleted]]
Hi Adam, Possibly subset() or & would be helpful. Or even aggregate(), depending on your ultimate goal. Without a reproducible example that includes some sample data provided using dput() (fake is fine), the code you used, and some clear idea of what output you expect, it's impossible to figure out how to help you. Here are some suggestions for creating a good reproducible example: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example Sarah On Fri, Jul 31, 2015 at 2:49 PM, Adam Jauregui <adma89 at gmail.com> wrote:> Hello R-help, > > I am trying to compute the mean of a quarterback's career fantasy football > stats, but I wish to exclude his 2014 stats from the mean, as that will be > the test data for the model I am trying to build for my academic undergrad > research. > > The code for figuring out the mean of his Yds for every career Game 1 was > simple: > > > *mean(brady.t$Yds[brady.t$G. == 1])* > How can I make an "if-then" statement though so that his 2014 stats are > excluded? Or is there an easier way besides "if-then?" > > Thank you, > > AKJ > > [[alternative HTML version deleted]] >-- Sarah Goslee http://www.functionaldiversity.org
> On Jul 31, 2015, at 1:49 PM, Adam Jauregui <adma89 at gmail.com> wrote: > > Hello R-help, > > I am trying to compute the mean of a quarterback's career fantasy football > stats, but I wish to exclude his 2014 stats from the mean, as that will be > the test data for the model I am trying to build for my academic undergrad > research. > > The code for figuring out the mean of his Yds for every career Game 1 was > simple: > > > *mean(brady.t$Yds[brady.t$G. == 1])* > How can I make an "if-then" statement though so that his 2014 stats are > excluded? Or is there an easier way besides "if-then?" > > Thank you, > > AKJIt would be helpful to have a sample of data to know the structure, but take a look at: ?subset for examples of more complicated logic for subsetting data frames. You might be able to use something along the lines of: mean(subset(brady.t, (G. == 1) & (Year != 2014), select = Yds)[[1]]) Basically, subset() is returning a data frame where Year does not equal 2014 and G. is equal to 1. The select argument is only returning the Yds column, which would otherwise be a list, so the [[1]] only returns a vector, which is passed to mean(). Regards, Marc Schwartz
This is very basic. Have you made **any** effort to learn R -- e.g. by going through an R tutorial? If not, please do this before posting further. This will save you -- and foks on this list, probably -- a lot of grief in the long (or even short) run. Also, if/when you do post further, post in plain text, not HTML, as requested by the posting guide below. Cheers, Bert Bert Gunter "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." -- Clifford Stoll On Fri, Jul 31, 2015 at 11:49 AM, Adam Jauregui <adma89 at gmail.com> wrote:> Hello R-help, > > I am trying to compute the mean of a quarterback's career fantasy football > stats, but I wish to exclude his 2014 stats from the mean, as that will be > the test data for the model I am trying to build for my academic undergrad > research. > > The code for figuring out the mean of his Yds for every career Game 1 was > simple: > > > *mean(brady.t$Yds[brady.t$G. == 1])* > How can I make an "if-then" statement though so that his 2014 stats are > excluded? Or is there an easier way besides "if-then?" > > Thank you, > > AKJ > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.