Reputation: 49
I have a longitudinal dataset in which people are turning 40 in different years, and I need to do an analysis (propensity score matching) with the 40 year-olds. I want to create an income variable which would use Income 1992
for people who turn forty in 1998, uses Income 1994
for people who turn forty in 2000 and so on.
My data looks like this (and I want Incomenew to look like this):
ID | SourceYear| Income1992| Income1994 | Incomenew |
|---------------|------------|------------| |
| 1 | 1998 | 10000 | 12000 | 10000 |
| 2 | 2000 | 20000 | 15000 | 15000 |
| 3 | 1998 | 17000 | 16000 | 17000 |
| 4 | 2000 | 18000 | 20000 | 20000 |
I am interested in their income 6 years before they turn 40. I already adjusted all income variables for the purchasing power of a certain year.I tried this:
Incomenew<-NA
Incomenew[SourceYear=="1998"]<-Income1992[SourceYear=="1998"]
Incomenew[SourceYear=="2000"]<-Income1994[SourceYear=="2000"]
I get all NAs
I also tried this:
`Incomenew<-if (SourceYear=="1998")] {Income1992}
else if (SourceYear==2000)
{Income1994}`
I get the following error
Error in if (SourceYear== "1998") { : argument is of length zero
It would be of great help if someone could help with this, I would really appreciate it.
Upvotes: 1
Views: 142
Reputation: 49
In my original dataset I had some NA's for the SourceYear. I didn't realize that it was important for this command. The first command actually works, if a subset without NA's in the SourceYear is used. An example is:
ID<-c(1,2,3,4,5,6)
SourceYear<-c("1998", "2000", "1998","2002","2000", "2002", NA)
Income92<-c(100000,120000,170000,180000, 190000, NA)
Income94<-c(120000,150000,160000,20000,NA, 120000)
Income96<-c(130000, 110000,NA, 180000, 190000, 180000)
incomedata<-data.frame(ID, SourceYear,Income92, Income94, Income96, Incomenew)
summary(incomedata)
incomedata1<-subset(incomedata, !is.na(incomedata$SourceYear))
incomedata1$Incomenew<-rep(NA, length(incomedata1$SourceYear))
incomedata1$Incomenew[incomedata1$SourceYear=="1998"]<-
incomedata1$Income92[incomedata1$SourceYear=="1998"]
incomedata1$Incomenew[incomedata1$SourceYear=="2000"]<-
incomedata1$Income94[incomedata1$SourceYear=="2000"]
incomedata1$Incomenew[incomedata1$SourceYear=="2002"]<-
incomedata1$Income96[SourceYear=="2002"]
Upvotes: 1