Identifying where value changes in R data.frame column and generate new column

I want new column(New) with 1 and 0 value .1 and 0 will be generates when the value changes.
Thanks.

Value New
5 0
5 0
6 1
5 1
2 1
2 0
2 0
2 0
2 0
10 1

(start_df <- start_df2 <- data.frame(
  v = c(5L, 5L, 6L, 5L, 2L, 2L, 2L, 2L, 2L, 10L),
  goal = c(0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 1L)
))

# in base R

start_df$new <- 1L * (c(NA, start_df$v[-length(start_df$v)]) != start_df$v)
start_df$new[is.na(start_df$new)] <- 0L
start_df

# with dplyr/tidyverse
library(tidyverse)
(start_df2 <- mutate(start_df2,
                      new = 1L * (lag(v) != v)
                    ) |>
                      replace_na(list(new = 0L)))

lag requires adjustment of the initial entry, because there is nothing to compare the first 5 to and that is true of this base solution as well

v <- c(5, 5, 6, 5, 2, 2, 2, 2, 2, 10)
goal <- c(0, 0, 1, 1, 1, 0, 0, 0, 0, 1)
new <- ifelse(c(0,diff(v)) == 0,0,1)
identical(goal,new)
#> [1] TRUE

Created on 2023-09-26 with reprex v2.0.2

theres a base::diff() ? nice ! ; I worked too hard doing it manually in base R :smiley:

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.