I would like to apply a function for distinct df id's, in which if next row is distinct of previous one then return 1 else return 0.
I have completely no clue of how could I achieve this, Is there anyway to accomplish it?
Here's the data structure:
df <- structure(list(
id = c(2,2,3,3,4,4,5,5),
job_position = c("Analyst", "Supervisor",
"HRBP", "HRBP", "Economist",
"Financial Planner", "Reporter",
"Reporter")),
class = "data.frame",
row.names = c(NA, -8L))
))
# id job_position
# 2 Analyst
# 2 Supervisor
# 3 HRBP
# 3 HRBP
# 4 Economist
# 4 Financial Planner
# 5 Reporter
# 5 Reporter
Expected output should look like this:
# id job_position changing_job_position
# 2 Analyst 0
# 2 Supervisor 1
# 3 HRBP 0
# 3 HRBP 0
# 4 Economist 0
# 4 Financial Planner 1
# 5 Reporter 0
# 5 Reporter 0