I now need to deal with a set of experimental data where the data trend goes up and down and up and down again, for example:
List = [0,1,2,3,4,3,2,3,4,5,6,7,6,4,5,8,9]
I want my dataset to end up looking like this:
AList = [0,1,2,3,4,(remove 3,2,3,4,) 5,6,7,*(remove 6,4,5,)*8,9]
which is
AList = [0,1,2,3,4,5,6,8,9]
Does everyone have any good ideas?
I tried to use a for loop like:
AList=matrix(ncol=1)
list <- c(0,1,2,3,4,3,2,3,4,5,6,7,6,4,5,8,9)
for (i in seq(1, length(list))){
if (list[i] < list[i+1]) {
AList[i] = list[i]
} else {
list[i]=list[i+1]
}
}
AList
But I get an error message like this:
Error in if (list[i] <= list[i + 1]) { :
missing value where TRUE/FALSE needed
Then I changed it to this:
for (i in seq(1, length(list))){
if (list[i] < is.na(list[i+1])) {
AList[i] = list[i]
} else {
list[i]=is.na(list[i+1])
}
}
Then I get a result that is exactly the same as the list.
Filter to values greater than the lagged cumulative maximum:
library(dplyr)
list1[list1 > dplyr::lag(cummax(list1), default = -Inf)]
# 0 1 2 3 4 5 6 7 8 9