[R] lag function row name with

Eric Berger er|cjberger @end|ng |rom gm@||@com
Mon Mar 25 09:16:10 CET 2019


Hi John,
dplyr::lag expects a vector. The following should work

dplyr::lag(temp[,1],2)

HTH,
Eric


On Mon, Mar 25, 2019 at 9:45 AM John <miaojpm using gmail.com> wrote:

> Hi,
>
>    I have a dataset whose row names corresponds to months. When I apply lag
> function (dplyr package) on this dataset, I get NAs with warning messages.
> Is there any lag function that carries out the lag but keep the row names?
>    I will have two datasets. The dates of the datasets are not exactly the
> same, and I want to find out the correlation for the overlapping period.
>
>    Thanks,
>
>
> > temp
>          oil95
> 1981M01 103.27
> 1981M02 107.92
> 1981M03 110.26
> 1981M04 110.26
> 1981M05 110.11
> 1981M06 109.93
> 1981M07 109.93
> 1981M08 109.93
> 1981M09 109.93
> 1981M10 109.93
>
> > dplyr::lag(temp, 2)
>         oil95
> 1981M01    NA
> 1981M02  <NA>
> 1981M03  <NA>
> 1981M04  <NA>
> 1981M05  <NA>
> 1981M06  <NA>
> 1981M07  <NA>
> 1981M08  <NA>
> 1981M09  <NA>
> 1981M10  <NA>
> Warning message:
> In format.data.frame(x, digits = digits, na.encode = FALSE) :
>   corrupt data frame: columns will be truncated or padded with NAs
> >
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list