2014-09-20 82 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