I have a Data table as:
What I need to find (for each NAME)
a. Rows when consecutive data is 0 in the column "Value1" : Shown in Red
b. Once identified, get the value of "Value2" from the next row. : Shown in Green
I believe I could use the package rle() but I am struggling to get the data per "Name"
DF <- readxl::read_excel("test.xlsx")
data.table::setDT(DF)
rle(DF$Value1)
Above statement would provide Length and Values. How do I get this data and position per NAME.
dput:
structure(list(Name = c("A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "B", "B", "B",
"B", "B", "B"), Date = structure(c(946684800, 946771200, 946857600,
946944000, 947030400, 947116800, 947203200, 947289600, 947376000,
947462400, 947548800, 947635200, 946684800, 946771200, 946857600,
946944000, 947030400, 947116800, 947203200, 947289600, 947376000,
947462400, 947548800, 947635200), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), Value1 = c(1, 2, 0, 0, 10, 20, 0, 0, 0, 50,
10, 20, 0, 0, 1, 2, 10, 20, 0, 0, 0, 50, 10, 20), Value2 = c(5,
10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 5, 10, 15, 20, 25,
30, 35, 40, 45, 50, 55, 60)), row.names = c(NA, -24L), class = c("tbl_df",
"tbl", "data.frame"))
