1

I would like to study a trend of NDVI data for a specific time interval using bfast package.

For this final aim, I read about the necessity to convert my data frame into a time series object in R. For this operation I choose to use packages xts and zoo. After loading data (they are contained into a CSV file)

I have this situation. Here I reports the first rows of my database:

OBJECT_ID         date      mean     stdDev       min       max    median        
 1 2010-01-01T00:00:00 0.6080465 0.05773068 0.5156774 0.6311388 0.6311388
 1 2010-01-09T00:00:00 0.5557563 0.02051920 0.5229256 0.5639640 0.5639640       
 1 2010-01-17T00:00:00 0.4982911 0.05900599 0.4038815 0.5218935 0.5218935         
 1 2010-01-25T00:00:00 0.4447075 0.11671929 0.3980198 0.6314584 0.3980198
 1 2010-02-02T00:00:00 0.5195212 0.00000000 0.5195212 0.5195212 0.5195212
 1 2010-02-10T00:00:00 0.6116505 0.00000000 0.6116505 0.6116505 0.6116505

Data are referee to different polygon so for example I selected the "Object_ID"=1 and after I tried with this function to convert dataframe into time series object:

ts <-xts(df[,-1],order.by = as.Date(df$date,format='%F'))

Instead, the results:

    date         mean         stdDev        min           max          median      
2010-01-01 "2010-01-01" "0.60804648" "0.057730685" " 0.51567738" "0.63113875" "0.63113875"
2010-01-09 "2010-01-09" "0.55575628" "0.020519201" " 0.52292556" "0.56396396" "0.56396396"
2010-01-17 "2010-01-17" "0.49829110" "0.059005990" " 0.40388151" "0.52189349" "0.52189349"
2010-01-25 "2010-01-25" "0.44470752" "0.116719292" " 0.39801980" "0.63145839" "0.39801980"
2010-02-02 "2010-02-02" "0.51952123" "0.000000000" " 0.51952123" "0.51952123" "0.51952123"
2010-02-10 "2010-02-10" "0.61165048" "0.000000000" " 0.61165048" "0.61165048" "0.61165048"

why I have double columns for date and why my value has quotation marks?

  • Images are an inefficient way to present text information, both from a legibility standpoint (impossible to read on some devices), and from the inconvenience of forcing each of the volunteers who would assist you to retype your data. Please edit the question to contain ASCII input and output. – Vince Aug 27 '18 at 11:12
  • You need to share more information. One way could be to use xts and transform to ts, but bfast need at least two periods and you are sharing less than 3 months – aldo_tapia Aug 27 '18 at 17:28
2

Not sure what you are trying to accomplish here. If it is simply to sort by date you do not need the xts package and can just use bracket indexing to resort the data.frame.

( x <- data.frame(ID = c(rep(1,5), rep(2,5),rep(5,5)), 
                 date = seq(as.Date("2010-01-01",format='%F'), 
                 as.Date("2010-01-05",format='%F'),length.out=5),
                 y=runif(15)) )
x[order(as.Date(x$date, format='%F')),]

If you need it by sorted by ID you can specify multiple columns, in order of sorting order. Let's mix up x and then sort by ID and then date.

( x <- x[sample(1:nrow(x)),] )
( x <- x[order(x$ID, x$date),] )

For more complex operations, you can also split the data into a list object then use lapply to apply a function and do.call to reassemble the data.frame object. Here is an example with the simple bracket index sorting.

( x.split <- split(x,f=x$ID) )
( x.split <- lapply(x.split, FUN=function(x) x[order(as.Date(x$date, format='%F')),] ) )
( x <- do.call("rbind", x.split) )

For analysis of time-series raster data, I would recommend using the bfastSpatial package as it has wrapper functions for bfast and BFASTMonitor that simplifies the application of this model on spatial data.

Specifically, take a look at the bfmSpatial function. Here is a nice tutorial on pre-processing functionality and data analysis of raster time-series data using bfmPixel and bfmSpatial.

  • Thanks a lot for your answer. I'm trying to better explaination. My situation is similar to this script: df <- data.frame(ID = c(rep(1,368)), date = seq(as.Date("2010-01-01",format='%F'), as.Date("2017-12-31",format='%F'),length.out=368), y=runif(368)). I would like to convert this object into time series object, how can I do? Is it correct this command: xts(df[,-2],order.by = as.Date(df$date,format='%F')) thanks – Matpec33 Aug 28 '18 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.