# Create sample vector
v1 <- c(1, 1, 1, 2, 2, 3, 3, 3, 1, 1)
# Identify changed values
res1 <- changed(v1)
# View results
res1
# [1] TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE
# Create sample data frame
v2 <- c("A", "A", "A", "A", "A", "A", "B", "B", "B", "B")
dat <- data.frame(v1, v2)
# View original data frame
dat
# v1 v2
# 1 1 A
# 2 1 A
# 3 1 A
# 4 2 A
# 5 2 A
# 6 3 A
# 7 3 B
# 8 3 B
# 9 1 B
# 10 1 B
# Get changed values for each column
res2 <- changed(dat)
# View results
res2
# v1.changed v2.changed
# 1 TRUE TRUE
# 2 FALSE FALSE
# 3 FALSE FALSE
# 4 TRUE FALSE
# 5 FALSE FALSE
# 6 TRUE FALSE
# 7 FALSE TRUE
# 8 FALSE FALSE
# 9 TRUE FALSE
# 10 FALSE FALSE
# Get changed values for all columns
res3 <- changed(dat, simplify = TRUE)
# View results
res3
# [1] TRUE FALSE FALSE TRUE FALSE TRUE TRUE FALSE TRUE FALSE
# Get last items in each group instead of first
res4 <- changed(dat, reverse = TRUE)
# View results
res4
# v1.changed v2.changed
# 1 FALSE FALSE
# 2 FALSE FALSE
# 3 TRUE FALSE
# 4 FALSE FALSE
# 5 TRUE FALSE
# 6 FALSE TRUE
# 7 FALSE FALSE
# 8 TRUE FALSE
# 9 FALSE FALSE
# 10 TRUE TRUE
Run the code above in your browser using DataLab