The best way to find out is to experiment with it, since it is an interactive environment, and it is easy to create dummy data.
As for comparisons in adjacent lines, the easiest way is to use the - operator (which means โexclude this indexโ) to exclude the first and last lines, as in this example:
a <- 1:10 a[5] <- 0 a[-1] > a[-length(a)] # compare each row with the preceding value
If you want to make an if , you have two options:
1) if command evaluates only one value, so you need to make sure that it evaluates to TRUE / FALSE (for example, use all or any functions):
if(all(a[-1] > a[-length(a)])) { print("each row is incrementing") } else { print(paste("the",which(c(FALSE, a[-1] <= a[-length(a)])),"th row isn't incrementing")) }
2) You can make a vector if statement using the ifelse function. See help("ifelse") . Here is an example:
ifelse(a[-1] > a[-length(a)], 1, 0)
source share