I have a simple set of steps to reformat some data. These steps work (Finding the differential and then isolating those above or below one standard devation).
x <- x[!is.na(x)]
x.d <- diff(x)
x.d <- x.d[!is.na(x.d)]
x.d <- ifelse(abs(x.d) > sd(x.d,na.rm = TRUE),x.d, 0)
I have a lot of series, and so want to put the lines into a function, and call with lapply. e.g.
data.prep <- function(x) {
<The lines above>
return(x.d) }
I attached the data and created a list with the series name(s) in it - "world" in this case and called the function
mydatalist <- as.list("world")
mydatalist.d <- lapply(mydatalist, FUN = data.prep)
and I get
Error in abs(x.d) : non-numeric argument to mathematical function
3. ifelse(abs(x.d) > sd(x.d, na.rm = TRUE), x.d, 0) at FdataPrep.R#5
2. FUN(X[[i]], ...)
1. lapply(mydatalist, FUN = data.prep)
This is a link to what I'm trying to get. Input data versus wanted output The graph on the left is the raw data, the graph on the right is output I want.
Can somebody please help understand where I'm going wrong?
Thank you,
John