I am trying to get the values of a variable (B) that cames from the lag position given by other variable (A).
The variables are something like this:
# A B
# 1: 1 10
# 2: 1 20
# 3: 1 30
# 4: 1 40
# 5: 2 50
I want the output (C) to be like this, the first value woud be zero and the condition start in the second row:
# A B C
# 1: 1 10 0
# 2: 1 20 10
# 3: 1 30 20
# 4: 2 40 20
# 5: 2 50 30
I have done it with loops but because it´s a large amount of information is a lot of time to wait. I hope someone could give me an idea.
CodePudding user response:
Here's a way with dplyr
:
library(dplyr)
x %>%
mutate(
C = c(0, B[(2:n()) - A[-1]])
)
# A B C
# 1: 1 10 0
# 2: 1 20 10
# 3: 1 30 20
# 4: 2 40 20
# 5: 2 50 30
It translates directly to data.table
(with your colons in row names, I thought you might be using that package)
library(data.table)
dt = as.data.table(x)
dt[, C := c(0, B[(2:.N) - A[-1]])]
dt
# A B C
# 1: 1 10 0
# 2: 1 20 10
# 3: 1 30 20
# 4: 2 40 20
# 5: 2 50 30
Using this data:
x = read.table(text =' A B
1: 1 10
2: 1 20
3: 1 30
4: 2 40
5: 2 50', header = T)