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.
Thank you ... can you explain what you've done? -- View this message in context: http://r.789695.n4.nabble.com/Mean-Help-tp4648000p4648013.html Sent from the R help mailing list archive at Nabble.com.
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)) > xheight 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.eyesheight 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.
Seemingly Similar Threads
- [LLVMdev] RFC: llvm-shlib-test (Was: [llvm] r191029 - llvm-c: Make LLVMGetFirstTarget a proper prototype)
- How to change dataframe to tables
- odds ratios for n-way tables: seeking an *apply-able method
- how to make automatically each level from data.frame to vector
- 2 Ethernet cabling question