简体   繁体   中英

Row wise comparison of a dataframe in R

I have a data frame with multiple data points corresponding to each ID. When the status value is different between 2 timepoints for an ID, I want to flag the first status change. How do I achieve that in R? Below is a sample dataset.

ID Time Status
ID1 0 X
ID1 6 X
ID1 12 Y
ID1 18 Z

Result dataset

ID Time Status Flag
ID1 0 X
ID1 6 X
ID1 12 Y 1
ID1 18 Z

Here is a base R solution with ave . It creates a vector y that is equal to 1 every time the previous value is different from the current one. Then the Flag is computed with diff .

y <- with(df1, ave(Status, ID, FUN = function(x) c(0, x[-1] != x[-length(x)])))
df1$Flag <- c(0, diff(as.integer(y)) != 0)

df1
#   ID Time Status Flag
#1 ID1    0      X    0
#2 ID1    6      X    0
#3 ID1   12      Y    1
#4 ID1   18      Z    0

Data

df1 <- read.table(text = "
ID  Time    Status
ID1     0   X
ID1     6   X
ID1     12  Y
ID1     18  Z                  
", header = TRUE)

You can use mutate() with ifelse() and lag(), then replace the non-first Flag==1 with 0s with replace():

df1%>%group_by(ID)%>%
        mutate(Flag=ifelse(is.na(lag(Status)), 0,
                          as.integer(Time!=lag(Time) & Status!=lag(Status))))%>%
        group_by(ID, Flag)%>%
        mutate(Flag=replace(Flag, Flag==lag(Flag) & Flag==1, 0))

# A tibble: 4 x 4
# Groups:   ID, Flag [2]
  ID     Time Status  Flag
  <fct> <int> <fct>  <dbl>
1 ID1       0 X          0
2 ID1       6 X          0
3 ID1      12 Y          1
4 ID1      18 Z          0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM