2014-09-20 140 views
0

我試圖創建一個名爲'period'的會計年度變量,該變量將從9月到8月運行六年。我的數據幀 'DAT' 的結構如下:將兩個變量重新編碼爲一個新變量

'data.frame': 52966 obs. of 4 variables: 
$ userid  : int 96 96 96 101 101 101 101 101 101 101 ... 
$ comment.year : int 2008 2009 2009 2008 2008 2008 2008 2008 2008 2009 ... 
$ comment.month: int 7 3 8 7 8 9 10 11 12 1 ... 
$ num.comments : int 1 1 1 33 51 16 27 29 40 39 ... 

我收到此錯誤信息:錯誤:意外的 '=' 「逸$期[comment.year = 2008 & comment.month =」 當我運行以下代碼。我已經嘗試了雙等號,並將月份和年份整數放在引號中,但沒有成功。我也想知道是否有更簡單的方法來做recode。由於我正在處理6年,我的方法需要72行。

dat$period[comment.year=2008 & comment.month=9]<-"1"  
dat$period[comment.year=2008 & comment.month=10]<-"1"     
dat$period[comment.year=2008 & comment.month=11]<-"1" 
dat$period[comment.year=2008 & comment.month=12]<-"1" 
dat$period[comment.year=2009 & comment.month=1]<-"1" 
dat$period[comment.year=2009 & comment.month=2]<-"1" 
dat$period[comment.year=2009 & comment.month=3]<-"1" 
dat$period[comment.year=2009 & comment.month=4]<-"1" 
dat$period[comment.year=2009 & comment.month=5]<-"1" 
dat$period[comment.year=2009 & comment.month=6]<-"1" 
dat$period[comment.year=2009 & comment.month=7]<-"1" 
dat$period[comment.year=2009 & comment.month=8]<-"1" 
dat$period[comment.year=2009 & comment.month=9]<-"2" 
dat$period[comment.year=2009 & comment.month=10]<-"2"      
dat$period[comment.year=2009 & comment.month=11]<-"2" 
dat$period[comment.year=2009 & comment.month=12]<-"2" 
+1

要使它更容易[重現](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)你r問題,給了我們一個'dput'而不是'str()'。因爲你想測試的是平等而不是分配,所以在索引中使用'=='(即'dat $ period [comment.year == 2008&comment.month == 9] < - 「1」') – MrFlick 2014-09-20 03:10:57

+0

謝謝,不知道dput。很有用。我試過dat $ period [comment.year == 2008&comment.month == 9] < - 「1」,但在dat $ period [comment.year == 2008&comment.month == 9]中得到錯誤< - < 「1」:object'comment.year'not found – user3614783 2014-09-20 11:35:55

+1

其實它應該是'dat $ period [dat $ comment.year == 2008&dat $ comment.monar == 9] < - 「1」' – MrFlick 2014-09-20 13:41:33

回答

2

而不是做一堆的部分任務,爲什麼不計算不同的年份與月份> = 9的獎金凹凸?

#sample data 
dat<-data.frame(
    comment.year=rep(2009:2011, each=12), 
    comment.month=rep(1:12, 3) 
)[-(1:8), ] 

#assign new period 
dat$period<- dat$comment.year-min(dat$comment.year) + ifelse(dat$comment.month>=9,1,0) 

如果你想確保在某個用戶啓動,讓你

comment.year comment.month period 
9   2009    9  1 
10   2009   10  1 
11   2009   11  1 
12   2009   12  1 
13   2010    1  1 
14   2010    2  1 
15   2010    3  1 
16   2010    4  1 
17   2010    5  1 
18   2010    6  1 
19   2010    7  1 
20   2010    8  1 
21   2010    9  2 
22   2010   10  2 
23   2010   11  2 
24   2010   12  2 
25   2011    1  2 
26   2011    2  2 
27   2011    3  2 
28   2011    4  2 
29   2011    5  2 
30   2011    6  2 
31   2011    7  2 
32   2011    8  2 
33   2011    9  3 
34   2011   10  3 
35   2011   11  3 
36   2011   12  3 

,您可以使用2009而非min(dat$comment.year)

+0

Thanks,這工作很好,但我不明白ifelse部分如何工作。我知道9指的是9月,每個時期的開始,但不清楚表達的其餘部分。 – user3614783 2014-09-20 14:25:50

0

使用MrFlick的樣本數據:

dat$period = rep(1:3, each=12)[1:28] 
dat 
    comment.year comment.month period 
9   2009    9  1 
10   2009   10  1 
11   2009   11  1 
12   2009   12  1 
13   2010    1  1 
14   2010    2  1 
15   2010    3  1 
16   2010    4  1 
17   2010    5  1 
18   2010    6  1 
19   2010    7  1 
20   2010    8  1 
21   2010    9  2 
22   2010   10  2 
23   2010   11  2 
24   2010   12  2 
25   2011    1  2 
26   2011    2  2 
27   2011    3  2 
28   2011    4  2 
29   2011    5  2 
30   2011    6  2 
31   2011    7  2 
32   2011    8  2 
33   2011    9  3 
34   2011   10  3 
35   2011   11  3 
36   2011   12  3 
> 

可以很容易地擴展到您的數據。

0

我想你也可以嘗試(使用@ MrFlick的數據)

set.seed(42) 
dat1 <- dat[sample(1:nrow(dat)),] 
dat<- within(dat, {period<- as.numeric(factor(comment.year)) 
       period[comment.month <9] <- period[comment.month <9] -1}) 

dat 
#  comment.year comment.month period 
#9   2009    9  1 
#10   2009   10  1 
#11   2009   11  1 
#12   2009   12  1 
#13   2010    1  1 
#14   2010    2  1 
#15   2010    3  1 
#16   2010    4  1 
#17   2010    5  1 
#18   2010    6  1 
#19   2010    7  1 
#20   2010    8  1 
#21   2010    9  2 
#22   2010   10  2 
#23   2010   11  2 
#24   2010   12  2 
#25   2011    1  2 
#26   2011    2  2 
#27   2011    3  2 
#28   2011    4  2 
#29   2011    5  2 
#30   2011    6  2 
#31   2011    7  2 
#32   2011    8  2 
#33   2011    9  3 
#34   2011   10  3 
#35   2011   11  3 
#36   2011   12  3 

使用無序dat1

within(dat1, {period<- as.numeric(factor(comment.year)); period[comment.month <9] <- period[comment.month <9] -1})[,3] 
#[1] 3 3 1 2 2 1 2 1 2 2 1 2 2 1 1 2 2 1 1 1 3 1 2 1 2 1 2 3 

交叉檢查與@ MrFlick的方法的結果

dat1$comment.year-min(dat1$comment.year) + ifelse(dat1$comment.month>=9,1,0) 
# [1] 3 3 1 2 2 1 2 1 2 2 1 2 2 1 1 2 2 1 1 1 3 1 2 1 2 1 2 3 
相關問題