Turn time series into base 100 on R

Asked

Viewed 104 times

-1

Is there a function that converts an entire time series to base 100 on the R? If it does not exist, what would be the solution to the problem? Thank you

  • 1

    You can clarify the question with an example of data and expected output?

  • I have a time series and I’d like to divide all the values by the first and multiply by 100. Thus the first element would be 100 because it is divided by itself and multiplied by 100, the second value also serial divided by the first and multiplied by 100, the third would also be divided by the first and multiplied by 100 and so on. I don’t know if I’ve made myself clear?

1 answer

2


There is no R function to do this, it is therefore necessary to define a.

base100 <- function(x, na.rm = FALSE){
  if(na.rm) x <- na.omit(x)
  100*x/x[1]
}

set.seed(1234)
y <- as.ts(cumsum(rnorm(20)))

base100(y)

Note that if x[1] is equal to zero, the function gives error. I left anyway for the user to have the option to correct this case by case, as it thinks best.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.