0

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

Tech Commodities
  • 1,884
  • 6
  • 13
  • 1
    `mydatalist` is a list with a single element in it: the string `"world"`. When you pass that string through your `data.prep` function, it eventually tries to take an absolute value, which is not well-defined for strings. – Artem Sokolov Mar 23 '18 at 16:47
  • Is `world` a different variable? What's the desired output here? Maybe you wanted `mydatalist <- list(world)`? When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Mar 23 '18 at 16:52
  • "world" would be a data series in a vector, for example "world annual population" 50 numerical values. – Tech Commodities Mar 23 '18 at 17:00
  • I've added a link to the output of the working graph. That shows the raw data and the desired output. – Tech Commodities Mar 23 '18 at 17:06
  • 1
    Just a clarification on what @MrFlick wrote: when you quote "world" you are passing it as a string not a variable. Try passing `world` not `"world"`. – GordonShumway Mar 23 '18 at 18:25
  • Thank you for the help and the clarification. @MrFlick pointed me to **a** solution. I pass the function a string as before, e.g. "world", but then use get() to grab the data. Why I can't just pass the data series, I don't know - I tried many things, but they would all fail. Thank you for all the help. – Tech Commodities Mar 27 '18 at 21:34

0 Answers0