Using rle
:
apply(
crop2[2:7],
1,
function(x) all(tail(rle(x)$values,2) == c("Grassland","Cropland"))
)
#[1] FALSE FALSE FALSE FALSE TRUE FALSE
A function is apply
-ed across each row (MARGIN=1
) of your yearly data as defined by the subset crop2[2:7]
.
rle(x)$values
returns the order of the changes in land use, e.g. row 5 is:
# 2004 2008
#"Grassland" "Cropland"
Wrapping this in tail(...,2)
just gives the change between the last 2 uses, which in this instance is just the same data again:
# 2004 2008
#"Grassland" "Cropland"
The all(... = c("Grassland","Cropland"))
bit just tests whether the land use finished as "Cropland"
after coming directly from "Grassland"
, which in this instance is TRUE