0

This may be a basic question, but I want to calculate the average bond wealth for seniors aged 65-74, and only for those with bonds (>0) and eliminate no replies (which is signaled by the 9999...).

mean(Bonds[Age>64 & Age<75 & Bonds>0 & Bonds<999999999999])

I'm getting an error message: longer object length is not a multiple of shorter object length

What am I doing wrong?

1 Answers1

0

It is not clear from the question how the data is shaped; I assume Bonds is a data.frame with two columns Bonds and Age. Then

idx <- Bonds$Age>64 & Bonds$Age<75 & Bonds$Bonds>0 & Bonds$Bonds<999999999999

indicates which rows fullfill the conditions and

mean(Bonds[idx, "Bonds"])

returns the mean of the column Bonds.

Karsten W.
  • 17,826
  • 11
  • 69
  • 103
  • Thank you very much - your replies made me think that my approach was ok, and the suggestions helped me figure out the problem. I was using two large datasets, whose variable names should have been the same (same survey from two different years), but the age variable name was changed by a letter, so I was grabbing one variable from one year, and the other from the other year - hence the warning (thank you, for the distinction, joran). The length suggestion showed me the problem, Ari. Thank you, Karsten, for showing me a new way to do it. – bonnie macdonald Jul 18 '12 at 01:40