I have a dataframe. Let's suppose that i have two columns. The first one contains height, the second one contains eye color that can be Green, Blue or Brown. I want to calculate the aritmetic mean of the height only for those people who have Blue eyes. How can I do it? Thank you for your availability. -- View this message in context: http://r.789695.n4.nabble.com/Mean-Help-tp4648000.html Sent from the R help mailing list archive at Nabble.com.
Mean Help
6 messages · Hard Core, R. Michael Weylandt, Jessica Streicher
Many forms, but I'll recommend this one this time around: aggregate(height ~ eyes, DATA, mean) See ?aggregate for an explanation of what aggregate() does and ?formula for an explanation of the tilde syntax. Note that this assumes your column names are "height" and "eyes." Adjust as needed. Michael
On Wed, Oct 31, 2012 at 1:18 PM, Hard Core <gioxc at hotmail.it> wrote:
I have a dataframe. Let's suppose that i have two columns. The first one contains height, the second one contains eye color that can be Green, Blue or Brown. I want to calculate the aritmetic mean of the height only for those people who have Blue eyes. How can I do it? Thank you for your availability. -- View this message in context: http://r.789695.n4.nabble.com/Mean-Help-tp4648000.html Sent from the R help mailing list archive at Nabble.com.
______________________________________________ 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.
please provide a bit of the dataframe in the future using the dput() function
x<-data.frame(height=1:10, color=sample(c("blue","green","brown"),10,replace=T))
x
height color 1 1 blue 2 2 green 3 3 blue 4 4 brown 5 5 blue 6 6 brown 7 7 green 8 8 brown 9 9 green 10 10 brown
blue.eyes <- x[x$color=="blue", ] blue.eyes
height color 1 1 blue 3 3 blue 5 5 blue should create a new dataframe with only those that have blue eyes, then
mean(blue.eyes$height)
[1] 3 should provide you with the mean of those.
On 31.10.2012, at 14:18, Hard Core wrote:
I have a dataframe. Let's suppose that i have two columns. The first one contains height, the second one contains eye color that can be Green, Blue or Brown. I want to calculate the aritmetic mean of the height only for those people who have Blue eyes. How can I do it? Thank you for your availability. -- View this message in context: http://r.789695.n4.nabble.com/Mean-Help-tp4648000.html Sent from the R help mailing list archive at Nabble.com.
______________________________________________ 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.
Thank you but with that formula i don't understand how R can select only the one with blue eyes -- View this message in context: http://r.789695.n4.nabble.com/Mean-Help-tp4648000p4648030.html Sent from the R help mailing list archive at Nabble.com.
On Wed, Oct 31, 2012 at 4:31 PM, Hard Core <gioxc at hotmail.it> wrote:
Thank you but with that formula i don't understand how R can select only the one with blue eyes
Assuming you're referring to my proposal of aggregate(height ~ eyes, DATA, mean) It's not blue eyes only: that will summarize height for each eye color by taking the mean. Then just select blue eyes and you're good to go. Finally, I note you're posting from Nabble. Please include context in your reply -- I don't believe Nabble does this automatically, so you'll need to manually include it. Most of the regular respondents on this list don't use Nabble -- it is a _mailing list_ after all -- so we don't get the forum view you do, only emails of the individual posts. Combine that with the high volume of posts, and it's quite difficult to trace a discussion if we all don't make sure to include context. I'm only following up because I have a hunch this involves my earlier reply, but it's really not clear. Cheers, Michael
Dear Michael ... You're a genius thak you very much really thank you !!! It worked!!! -- View this message in context: http://r.789695.n4.nabble.com/Mean-Help-tp4648000p4648038.html Sent from the R help mailing list archive at Nabble.com.