Home > Software design >  R function to count number of times when values changes
R function to count number of times when values changes

Time:11-10

I am new to R,
I have 3 columns named A1, A2, ChangeInA that looks like this in a dataset

A1 A2 ChangeInA
10 20 10
24 30 24
22 35 35
54 65 65
15 29 15

The column 'ChangeInA' is either (A1 or A2)

I want to determine the number of times the 3rd column ('ChangeInA') changes.
Is there any function in R to do that?

Let me explain: From the table, we can see that the 'ChangeInA' column switched twice, first at row 3 and it switched again at row 5 (note that 'ChangeInA' can only have values of A1 or A2) so I want an R function to print how many times the switch happened. I can see the change on the dataset but I need to prove it on R

Below is a code I tried from previous answers

change<- rleid(rawData$ChangeInA == rawData$A1)
  • This showed me all the ChangeInA
change<- max(rleid(rawData$ChangeInA == rawData$A1))
  • This showed me the maximum number in ChangeInA

CodePudding user response:

One option is to use rleid from data.table to keep track of when a change occurs in ChangeInA, which we can use on a conditional of whether ChangeInA is equal to A1. Then, we can just use max to get the total number of changes.

library(data.table)

max(rleid(df$ChangeInA == df$A1) - 1)

# 2

Or we could use dplyr with rleid:

library(dplyr)

df %>%
  mutate(rlid = rleid(A1 == ChangeInA) - 1) %>% 
  pull(rlid) %>%
  last()

Data

df <- structure(list(A1 = c(10L, 24L, 22L, 54L, 15L), A2 = c(20L, 30L, 
35L, 65L, 29L), ChangeInA = c(10L, 24L, 35L, 65L, 15L)), class = "data.frame", row.names = c(NA, 
-5L))
  • Related