Will most likely expose that I am new to R, but in SPSS, running lags is very easy. Obviously this is user error, but what I am missing?
很有可能会暴露我是新手,但是在SPSS中,运行滞后是很容易的。显然这是用户错误,但是我缺少什么呢?
x <- sample(c(1:9), 10, replace = T)
y <- lag(x, 1)
ds <- cbind(x, y)
ds
Results in:
结果:
x y
[1,] 4 4
[2,] 6 6
[3,] 3 3
[4,] 4 4
[5,] 3 3
[6,] 5 5
[7,] 8 8
[8,] 9 9
[9,] 3 3
[10,] 7 7
I figured I would see:
我想我会看到:
x y
[1,] 4
[2,] 6 4
[3,] 3 6
[4,] 4 3
[5,] 3 4
[6,] 5 3
[7,] 8 5
[8,] 9 8
[9,] 3 9
[10,] 7 3
Any guidance will be much appreciated.
我们将非常感谢您的指导。
10 个解决方案
#1
24
Another way to deal with this is using the zoo package, which has a lag method that will pad the result with NA:
另一种处理方法是使用zoo包,它有一个延迟方法,可以用NA填充结果:
require(zoo)
> set.seed(123)
> x <- zoo(sample(c(1:9), 10, replace = T))
> y <- lag(x, -1, na.pad = TRUE)
> cbind(x, y)
x y
1 3 NA
2 8 3
3 4 8
4 8 4
5 9 8
6 1 9
7 5 1
8 9 5
9 5 9
10 5 5
The result is a multivariate zoo object (which is an enhanced matrix), but easily converted to a data.frame via
结果是一个多变量动物园对象(增强的矩阵),但是很容易转换成data.frame
> data.frame(cbind(x, y))
#2
22
I had the same problem, but I didn't want to use zoo or xts, so I wrote a simple lag function for data frames:
我也有同样的问题,但我不想使用zoo或xts,所以我为数据帧编写了一个简单的滞后函数:
lagpad <- function(x, k) {
if (k>0) {
return (c(rep(NA, k), x)[1 : length(x)] );
}
else {
return (c(x[(-k+1) : length(x)], rep(NA, -k)));
}
}
This can lag forward or backwards:
这可能会前后滞后:
x<-1:3;
(cbind(x, lagpad(x, 1), lagpad(x,-1)))
x
[1,] 1 NA 2
[2,] 2 1 3
[3,] 3 2 NA
#3
14
lag
does not shift the data, it only shifts the "time-base". x
has no "time base", so cbind
does not work as you expected. Try cbind(as.ts(x),lag(x))
and notice that a "lag" of 1 shifts the periods forward.
延迟不会改变数据,它只会改变“时间基础”。x没有“时间基础”,所以cbind没有您期望的那样工作。试试cbind(as.ts(x)、lag(x)),注意到1的“lag”将期间向前移动。
I would suggesting using zoo
/ xts
for time series. The zoo
vignettes are particularly helpful.
我建议在时间序列中使用zoo / xts。动物园的小插图特别有用。
#4
6
lag()
works with time series, whereas you are trying to use bare matrices. This old question suggests using embed
instead, like so:
lag()适用于时间序列,而您正在尝试使用裸矩阵。这个老问题建议使用embed,如下所示:
lagmatrix <- function(x,max.lag) embed(c(rep(NA,max.lag), x), max.lag+1)
for instance
例如
> x
[1] 8 2 3 9 8 5 6 8 5 8
> lagmatrix(x, 1)
[,1] [,2]
[1,] 8 NA
[2,] 2 8
[3,] 3 2
[4,] 9 3
[5,] 8 9
[6,] 5 8
[7,] 6 5
[8,] 8 6
[9,] 5 8
[10,] 8 5
#5
3
Using just standard R functions this can be achieved in a much simpler way:
只要使用标准的R函数,就可以用一种简单得多的方式实现:
x <- sample(c(1:9), 10, replace = T)
y <- c(NA, head(x, -1))
ds <- cbind(x, y)
ds
#6
2
tmp<-rnorm(10)
tmp2<-c(NA,tmp[1:length(tmp)-1])
tmp
tmp2
#7
2
This should accommodate vectors or matrices as well as negative lags:
这应该适应矢量或矩阵以及负滞后:
lagpad <- function(x, k=1) {
i<-is.vector(x)
if(is.vector(x)) x<-matrix(x) else x<-matrix(x,nrow(x))
if(k>0) {
x <- rbind(matrix(rep(NA, k*ncol(x)),ncol=ncol(x)), matrix(x[1:(nrow(x)-k),], ncol=ncol(x)))
}
else {
x <- rbind(matrix(x[(-k+1):(nrow(x)),], ncol=ncol(x)),matrix(rep(NA, -k*ncol(x)),ncol=ncol(x)))
}
if(i) x[1:length(x)] else x
}
#8
0
Just get rid of lag. Change your line for y to:
只要摆脱滞后。将你的线改为:
y <- c(NA, x[-1])
#9
0
The easiest way to me now appears to be the following:
对我来说最简单的方法是:
require(dplyr)
df <- data.frame(x = sample(c(1:9), 10, replace = T))
df <- df %>% mutate(y = lag(x))
#10
0
a simple way to do the same may be copying the data to a new data frame and changing the index number. Make sure the original table is indexed sequentially with no gaps
一种简单的方法是将数据复制到新的数据帧并更改索引号。确保原始表被按顺序索引,没有间隔
e.g.
如。
tempData <- originalData
rownames(tempData) <- 2:(nrow(tempData)+1)
if you want it in the same data frame as the original use a cbind function
如果您想让它与原始的数据框架相同,请使用cbind函数
#1
24
Another way to deal with this is using the zoo package, which has a lag method that will pad the result with NA:
另一种处理方法是使用zoo包,它有一个延迟方法,可以用NA填充结果:
require(zoo)
> set.seed(123)
> x <- zoo(sample(c(1:9), 10, replace = T))
> y <- lag(x, -1, na.pad = TRUE)
> cbind(x, y)
x y
1 3 NA
2 8 3
3 4 8
4 8 4
5 9 8
6 1 9
7 5 1
8 9 5
9 5 9
10 5 5
The result is a multivariate zoo object (which is an enhanced matrix), but easily converted to a data.frame via
结果是一个多变量动物园对象(增强的矩阵),但是很容易转换成data.frame
> data.frame(cbind(x, y))
#2
22
I had the same problem, but I didn't want to use zoo or xts, so I wrote a simple lag function for data frames:
我也有同样的问题,但我不想使用zoo或xts,所以我为数据帧编写了一个简单的滞后函数:
lagpad <- function(x, k) {
if (k>0) {
return (c(rep(NA, k), x)[1 : length(x)] );
}
else {
return (c(x[(-k+1) : length(x)], rep(NA, -k)));
}
}
This can lag forward or backwards:
这可能会前后滞后:
x<-1:3;
(cbind(x, lagpad(x, 1), lagpad(x,-1)))
x
[1,] 1 NA 2
[2,] 2 1 3
[3,] 3 2 NA
#3
14
lag
does not shift the data, it only shifts the "time-base". x
has no "time base", so cbind
does not work as you expected. Try cbind(as.ts(x),lag(x))
and notice that a "lag" of 1 shifts the periods forward.
延迟不会改变数据,它只会改变“时间基础”。x没有“时间基础”,所以cbind没有您期望的那样工作。试试cbind(as.ts(x)、lag(x)),注意到1的“lag”将期间向前移动。
I would suggesting using zoo
/ xts
for time series. The zoo
vignettes are particularly helpful.
我建议在时间序列中使用zoo / xts。动物园的小插图特别有用。
#4
6
lag()
works with time series, whereas you are trying to use bare matrices. This old question suggests using embed
instead, like so:
lag()适用于时间序列,而您正在尝试使用裸矩阵。这个老问题建议使用embed,如下所示:
lagmatrix <- function(x,max.lag) embed(c(rep(NA,max.lag), x), max.lag+1)
for instance
例如
> x
[1] 8 2 3 9 8 5 6 8 5 8
> lagmatrix(x, 1)
[,1] [,2]
[1,] 8 NA
[2,] 2 8
[3,] 3 2
[4,] 9 3
[5,] 8 9
[6,] 5 8
[7,] 6 5
[8,] 8 6
[9,] 5 8
[10,] 8 5
#5
3
Using just standard R functions this can be achieved in a much simpler way:
只要使用标准的R函数,就可以用一种简单得多的方式实现:
x <- sample(c(1:9), 10, replace = T)
y <- c(NA, head(x, -1))
ds <- cbind(x, y)
ds
#6
2
tmp<-rnorm(10)
tmp2<-c(NA,tmp[1:length(tmp)-1])
tmp
tmp2
#7
2
This should accommodate vectors or matrices as well as negative lags:
这应该适应矢量或矩阵以及负滞后:
lagpad <- function(x, k=1) {
i<-is.vector(x)
if(is.vector(x)) x<-matrix(x) else x<-matrix(x,nrow(x))
if(k>0) {
x <- rbind(matrix(rep(NA, k*ncol(x)),ncol=ncol(x)), matrix(x[1:(nrow(x)-k),], ncol=ncol(x)))
}
else {
x <- rbind(matrix(x[(-k+1):(nrow(x)),], ncol=ncol(x)),matrix(rep(NA, -k*ncol(x)),ncol=ncol(x)))
}
if(i) x[1:length(x)] else x
}
#8
0
Just get rid of lag. Change your line for y to:
只要摆脱滞后。将你的线改为:
y <- c(NA, x[-1])
#9
0
The easiest way to me now appears to be the following:
对我来说最简单的方法是:
require(dplyr)
df <- data.frame(x = sample(c(1:9), 10, replace = T))
df <- df %>% mutate(y = lag(x))
#10
0
a simple way to do the same may be copying the data to a new data frame and changing the index number. Make sure the original table is indexed sequentially with no gaps
一种简单的方法是将数据复制到新的数据帧并更改索引号。确保原始表被按顺序索引,没有间隔
e.g.
如。
tempData <- originalData
rownames(tempData) <- 2:(nrow(tempData)+1)
if you want it in the same data frame as the original use a cbind function
如果您想让它与原始的数据框架相同,请使用cbind函数