Function for certain values in rows
I have a paneldata which looks like:
(Only th开发者_如何转开发e substantially cutting for my question)
Persno 122 122 122 333 333 333 333 333 444 444
Income 1500 1500 2000 2000 2100 2500 2500 1500 2000 2200
year 1 2 3 1 2 3 4 5 1 2
I need a command or function to recognize the different Person. For all rows with the same Person I would like to give out the average income.
Thank you very much.
My favorite tool to solve problems like this is ddply, in the plyr package.
library(plyr)
p = pdata.frame(data.frame(year=rep(c(1,2,3),3), persno = c(1,1,1,2,2,2,3,3,3), income=c(1500,1500,2000,2000,2100,2500,2500,1500,2000)))
dply(p, .(persno), summarize, mean.income = mean(income))
which gives us the output
persno mean.income
1 1 1666.667
2 2 2200.000
3 3 2000.000
精彩评论