2016-03-15 36 views
1

我有一個縱向數據集,在這個數據集中人們在不同年份的時間是40,我需要對40歲的人進行分析(傾向評分匹配)。我想創建一個收入變量,該值將在1998年變爲四十歲的人中使用Income 1992,在2000年變爲四十歲的人中使用Income 1994等等。在R中通過條件命令創建變量

我的數據是這樣的(我想Incomenew看起來像這樣):

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  | 

我很感興趣,他們的收入,他們轉6年前40.我已經調整了購買力的所有收入變量一定year.I的嘗試這樣做:

Incomenew<-NA 
Incomenew[SourceYear=="1998"]<-Income1992[SourceYear=="1998"] 
Incomenew[SourceYear=="2000"]<-Income1994[SourceYear=="2000"] 

我得到的所有NAS

我也試過這樣:

`Incomenew<-if (SourceYear=="1998")] {Income1992} 
        else if (SourceYear==2000) 
       {Income1994}` 

我收到以下錯誤

錯誤,如果(SourceYear == 「1998年」){:參數是長度爲零

這將是很大的幫助,如果有人能夠幫助有了這個,我真的很感激它。

+0

您需要顯示一個可重複的示例。此外,「收入新」只有長度1,而SourceYear的長度可能不同。嘗試'Incomenew < - rep(NA,length(SourceYear))' – akrun

+0

除非'SourceYear'保存爲對子集化有用的對象(我無法分辨沒有數據),否則它可能需要以數據集爲前綴: Income1992 [Income1992 $ SourceYear == 1998,]'。請注意,您還需要在它後面加一個逗號,以指定您的子集年份,並且需要所有列,並確定您的年份是字符串('「1998」')還是數字('2000')。 – alistaire

+0

@akrun非常感謝您的回答,同時嘗試創建一個可重複的示例,我發現此命令有效;但在我的原始數據中,它並沒有在第一次。然後我意識到這是因爲我在SourceYear中有一些NA。當我省略這些時,它就起作用了。謝謝! –

回答

1

在我的原始數據集中,我對SourceYear有一些NA。我沒有意識到這個命令很重要。 如果使用SourceYear中沒有NA的子集,則第一個命令實際上起作用。一個示例是:

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"]