I am looking to obtain a vector of T/F values based on whether I have reached the last row of data or not. I have attached an example data frame below.
df <- structure(list(A = 1:24, B = 2:25), class = "data.frame", row.names = c(NA,
-24L))
Here is my desired output given the provided data.
c("F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F",
"F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "T")
Would be great if this was a function similar to is.na such that I could include in an ifelse statement.
i.e.,
df$new_variable <- ifelse(if.last(df) == 'T' & df$B == 25, 0, df$B)
>Solution :
You can try this:
1:nrow(df) == nrow(df)
To make it a function:
is.last <- function(data) {
1:nrow(data) == nrow(data)
}
is.last(df)
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE