0

So I am trying to take percent changes of a list of numbers. However when I try to call upon the previous element it regards it as the current element minus 1. How may I fix this? Ex. list = (1,2,3), I want to return (n/a, 1, .5) [n/a, (2-1)/1, (3-2)/2]

percent_change <- function(a,b,c,d,e,f,g,h,i,j,k,l){
  dataList <- list(a,b,c,d,e,f,g,h,i,j,k,l)
  for(i in dataList){print((i-(i-1))/(i-1))}
}
biglig
  • 55
  • 6
  • YOu may need to use `for(i in 2:length(dataList)) {print(datList[[i]] - dataList[[i-1]])}` – akrun Apr 27 '20 at 00:17
  • Please provide reproducible data when you post, for example `datalist<-list(1,2,3)` and make sure everything that is code is formatted as code. – Nakx Apr 27 '20 at 02:49
  • Possible duplicate: https://stackoverflow.com/questions/21091202/how-to-index-an-element-of-a-list-object-in-r – Nakx Apr 27 '20 at 02:50

0 Answers0