Hello, I have a set of data including age, wage and education level each called age76, wage76 and grade76 I want to know how i can calculate the average wage of people age 15 to 65 (each year separetly) , only for those who have an education level of 10 12 and 16... -- View this message in context: http://r.789695.n4.nabble.com/Conditional-average-tp4585313p4585313.html Sent from the R help mailing list archive at Nabble.com.
Kebrab67-- It's difficult to answer in the absence of a small bit of representative data, and more information about it. How is age76 recorded, as numerical years or in categories (age ranges?) And what are years (you didn't mention them as a variable in your data.) That being said, perhaps by(), or summaryBy() in the doBy package, might help. --Chris Ryan SUNY Upstate Medical University Clincal Campus Binghamton, NY kebrab67 wrote:> Hello, I have a set of data including age, wage and education level each > called age76, wage76 and grade76 I want to know how i can calculate the > average wage of people age 15 to 65 (each year separetly) , only for those > who have an education level of 10 12 and 16... > > -- > View this message in context: http://r.789695.n4.nabble.com/Conditional-average-tp4585313p4585313.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.
In base R, you probably want either ave() or tapply() (depending on how you want your results formatted) for a quick and dirty solution. aggregate() will get you there and allow more powerful transformations with a little more work. All of these have examples in the help pages that should get you started. Michael On Tue, Apr 24, 2012 at 9:23 PM, kebrab67 <selamgetachew at gmail.com> wrote:> Hello, I have a set of data including age, wage and education level each > called age76, wage76 and grade76 I want to know how i can calculate the > average wage of people age 15 to 65 (each year separetly) , only for those > who have an education level of 10 12 and 16... > > -- > View this message in context: http://r.789695.n4.nabble.com/Conditional-average-tp4585313p4585313.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.
id age76 Wage76 Grade76 Black immigrt. ... 1 25 102456 12 1 0 2 27 15432 15 0 1 3 30 12340 16 1 1 Then I have lots of data variables 100 for 5000 individuals nearly. What I wanted is to discriminated by age and education level (age is years only and grade is years in educational system and wage is salary only no rational numbers here) I want to have for people of education level 10 12 16, the average wage for age 15, and then the same for age 16, and the same for age 17 and so on... Once i have these average I plot (age vs average salary) to see the "distribution in average" -- View this message in context: http://r.789695.n4.nabble.com/Conditional-average-tp4585313p4586691.html Sent from the R help mailing list archive at Nabble.com.
I think this will accomplish what you describe: dd <- data.frame(id=c(1:6), age76=c(25, 27, 30, 82, 20, 25), Wage76=c(102456, 15432, 12340, 6, 20000, 15000), Grade76=c(12, 15, 16, 88, 16, 12), Black=c(1, 0, 1, 0, 1, 1), imigrt=c(0, 1, 1, 0, 1, 1)) dd dd.sub <- subset(dd, Grade76 %in% c(12,15,16)) dd.sub #aggregate and summaryBy accomplish similar thing aggregate(data=dd.sub, Wage76 ~ age76, FUN=mean) library(doBy) #lattice gets loaded too summaryBy(data=dd.sub, Wage76 ~ age76) dd.means <- aggregate(data=dd.sub, Wage76 ~ age76, FUN=mean) xyplot(data=dd.means, Wage76 ~ age76) # lattice already loaded --Chris Ryan kebrab67 wrote:> id age76 Wage76 Grade76 Black immigrt. ... > 1 25 102456 12 1 0 > 2 27 15432 15 0 1 > 3 30 12340 16 1 1 > > Then I have lots of data variables 100 for 5000 individuals nearly. What I > wanted is to discriminated by age and education level (age is years only and > grade is years in educational system and wage is salary only no rational > numbers here) > I want to have for people of education level 10 12 16, the average wage for > age 15, and then the same for age 16, and the same for age 17 and so on... > Once i have these average I plot (age vs average salary) to see the > "distribution in average" > > -- > View this message in context: http://r.789695.n4.nabble.com/Conditional-average-tp4585313p4586691.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.