Reputation: 25
I need help with the below question in R language.
Lets say I have a data set:
X Y
1 1
2 2
3 3
4 4
How would I go about looping through the dataset subtracting the X value in the current row from the X value in the row below, then going to the second row etc?
Currently I have the below:
df <- (df[row(df)-1,1] - df[row(df)+1,1])
I would like to get the following:
X
-1
-1
-1
N/a
However it seems to being doing the calculations twice and I am getting?
X
-1
-1
-1
N/a
-1
-1
-1
N/a
I cant figure out why, any help would be appreciated?
Upvotes: 1
Views: 73
Reputation: 72683
You could use diff()
. Also you could do it using a matrix multiplication approach.
Example
set.seed(42)
x <- sample(10, 10, replace=TRUE)
> x
[1] 10 10 3 9 7 6 8 2 7 8
> diff(x)
[1] 0 -7 6 -2 -1 2 -6 5 1
# difference matrix approach
lbd <- matrix(0, nrow=length(x) - 1, ncol=length(x)) # setup lambda
diag(lbd) <- -1
diag(lbd[, -1]) <- 1
> lbd
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] -1 1 0 0 0 0 0 0 0 0
[2,] 0 -1 1 0 0 0 0 0 0 0
[3,] 0 0 -1 1 0 0 0 0 0 0
[4,] 0 0 0 -1 1 0 0 0 0 0
[5,] 0 0 0 0 -1 1 0 0 0 0
[6,] 0 0 0 0 0 -1 1 0 0 0
[7,] 0 0 0 0 0 0 -1 1 0 0
[8,] 0 0 0 0 0 0 0 -1 1 0
[9,] 0 0 0 0 0 0 0 0 -1 1
> lbd %*% x # matrix multiplication, same result as in `diff(x)` above
[,1]
[1,] 0
[2,] -7
[3,] 6
[4,] -2
[5,] -1
[6,] 2
[7,] -6
[8,] 5
[9,] 1
Using your data:
x1 <- 1:4
lbd1 <- matrix(0, nrow=length(x1) - 1, ncol=length(x1))
diag(lbd1) <- -1
diag(lbd1[, -1]) <- 1
> lbd1 %*% x1
[,1]
[1,] 1
[2,] 1
[3,] 1
> diff(x1) # same
[1] 1 1 1
Upvotes: 0
Reputation: 1784
As @Sotos pointed out, you can solve this with diff
.
But the reason this isn't working is because row()
returns row numbers for both columns
> row(df)
[,1] [,2]
[1,] 1 1
[2,] 2 2
[3,] 3 3
[4,] 4 4
If you select either column alone your code works:
df <- (df[row(df)[,1]-1,1] - df[row(df)[,1]+1,1])
Upvotes: 1