shrub
shrub

Reputation: 3

Reference previous row in a data.frame

I have a very simple problem. I'm am trying to set the value of column X to 0 if column Y[n,] does not equal column Y[n-1,]. My issue is that I do not know how to reference a previous row value in R, and then use that value to set the value of another column.

As an example:

Y X
1 5
1 1
2 0
2 2

X[3,2] is 0 because Y[3,1] does not equal Y[2,1].

I need to basically find all instance of this in a large data-set and set the corresponding X value to 0.

data$X <- 0 if data$Y[n] =! data$Y[n-1]

Is there a simple solution to this in R? It really feels as though there should be.

Thank you

Upvotes: 0

Views: 2907

Answers (3)

akrun
akrun

Reputation: 887981

Another base R option

with(df, X * c(TRUE, !(Y[-1] - Y[-length(Y)])))
#[1] 5 1 0 2

Or using dplyr

library(dplyr)
df %>% 
   mutate(X = c(X[1], ((duplicated(Y) * X)[-1])))
#  Y X
#1 1 5
#2 1 1
#3 2 0
#4 2 2

data

df <- structure(list(Y = c(1L, 1L, 2L, 2L), X = c(5L, 1L, 0L, 2L)), 
  class = "data.frame", row.names = c(NA, -4L))

Upvotes: 0

tmfmnk
tmfmnk

Reputation: 40181

Similarly to the post from @markus, with dplyr you can do:

df %>%
 mutate(X = (Y == lag(Y, default = first(Y))) * X)

  Y X
1 1 5
2 1 1
3 2 0
4 2 2

Upvotes: 1

markus
markus

Reputation: 26373

Given

Y <- c(1, 1, 2, 2)
X <- c(5, 1, 10, 2)

an option would be diff

X * (c(0, diff(Y)) == 0)
# [1] 5 1 0 2

The idea is to check if x[i] - x[i -1] equals zero which gives a logical vector that we multiply by X

Upvotes: 1

Related Questions