将多个变量添加到data.table

时间:2017-01-25 05:17:05

标签: r data.table

我的数据如下:

    gvkey   datadate fyear     cusip curcd     at    ceq  csho   dltt dvc  nopi oibdp prstkc
1:  1001 12/31/1981  1981 000165100   USD     NA     NA    NA     NA  NA    NA    NA     NA
2:  1001 12/31/1982  1982 000165100   USD     NA     NA    NA     NA  NA    NA    NA     NA
3:  1001 12/31/1983  1983 000165100   USD 14.080  7.823 3.568  4.344   0 0.640 2.650  0.000
4:  1001 12/31/1984  1984 000165100   USD 16.267  8.962 3.568  4.181   0 0.575 3.208  0.000
5:  1001 12/31/1985  1985 000165100   USD 39.495 13.014 3.988 11.908   0 0.623 7.247  0.009
6:  1003 12/31/1981  1981 000354100   USD     NA     NA    NA     NA  NA    NA    NA     NA
   pstkrv prcc_c year          REP
1:     NA     NA 1981           NA
2:     NA     NA 1982           NA
3:      0  7.250 1983           NA
4:      0  3.750 1984 0.0000000000
5:      0 10.125 1985 0.0002278769
6:     NA     NA 1981           NA

我想添加几个基于现有变量的变量,我的代码如下:

Compustat.1<-Compustat.1[,  `:=`(DIV= dvc/at,
                                REPR= REP/(REP+DIV),
                                PCASH= oibdp/at,
                                TCASH= nopi/at,
                                CASHVOL= data.table::shift(rollapply(PCASH,5,FUN=sd,fill=NA,align="right"),1,fill=NA),
                                SIZE= at,
                                LEV= dltt/at,
                                MB= prcc_c*csho), 
                                by=cusip]

我一直收到错误消息

 Error in `[.data.table`(Compustat.1, , `:=`(DIV = dvc/at, REPR = REP/(REP +  : 
  object 'DIV' not found

由于我已经在第一步定义了变量DIV,我无法弄清楚问题是什么。

2 个答案:

答案 0 :(得分:0)

我们可以在{}

中执行此操作
Compustat.1[, c("DIV", "REPR", "PCASH", "TCASH", "SIZE", "LEV", "MB")  := 
            {DIV <- dvc/at
            REPR <- REP/(REP+DIV)
            PCASH <- oibdp/at
            TCASH <- nopi/at
          #CASHVOL <- data.table::shift(rollapply(PCASH,5,FUN=sd,fill=NA,align="right"),1,fill=NA)
           SIZE <- at
           LEV <- dltt/at
           MB  <-  prcc_c*csho
          .( DIV, REPR, PCASH, TCASH, SIZE, LEV, MB)}, by=cusip]

答案 1 :(得分:0)

我认为问题在于,当DIV尚未创建时,您正在引用它。鉴于DIV的公式似乎很简单,我建议直接在REPR的公式中复制它。 PCASH也是如此。另请注意,分配Compustat.1不是必需的,因为&#39;:=&#39;创建变量:

Compustat.1[,  `:=`(DIV= dvc/at,
                    REPR= REP/(REP + dvc/at),
                    PCASH= oibdp/at,
                    TCASH= nopi/at,
                    CASHVOL= data.table::shift(rollapply(oibdp/at,5,FUN=sd,fill=NA,align="right"),1,fill=NA),
                    SIZE= at,
                    LEV= dltt/at,
                    MB= prcc_c*csho), 
                    by=cusip]

应该运作良好,