0

I'm using R and moveVis package of R to do some movement visualization. Below is the csv from where I import the data using read.csv

I'm having trouble converting the data.frame to moveStack using df2move

trackId,x,y,time,x1,x2,optional,sensor,timestamps
A34,19.00094708496841,72.8264388198447,2021-12-23 10:00:00,19.00094708496841,72.8264388198447,FALSE,unknown,2021-12-23 10:00:00
A34,18.986663359819435,72.84012881354482,2021-12-23 10:02:00,18.986663359819435,72.84012881354482,FALSE,unknown,2021-12-23 10:02:00
raw_data <- read.csv("mdata2.csv", header = TRUE)
m <- df2move(raw_data, proj = "+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs", x = "x1", y = "x2", time = as.POSIXct(raw_data$timestamps, format = "%Y-%m-%d %H:%M:%S", tz = "UTC"), track_id = "trackId")

Getting this error on running above code

Error: Column named '1640233800' cannot be found in 'df'

  • Please provide the data in `raw_data` via `dput`. – JKupzig Jan 03 '22 at 08:32
  • Are you sure the `time` specification is correct? Spec from doc: `time character, name of the column in df that represents timestamps. Timestamps need to be of class POSIXct.` – QHarr Jan 03 '22 at 08:37
  • @JKupzig here's the output from dput `structure(list(trackId = c("vipin", "vipin"), x = c(72.8409492130316, 72.8363572715711), y = c(18.9968003664781, 18.9958569245008), time = c("2021-12-23 10:00:00", "2021-12-23 10:02:00"), x1 = c(72.8409492130316, 72.8363572715711), x2 = c(18.9968003664781, 18.9958569245008 ), optional = c(FALSE, FALSE), sensor = c("unknown", "unknown" ), timestamps = structure(c(NA_real_, NA_real_), class = c("POSIXct", "POSIXt"), tzone = "UTC")), row.names = c(NA, -2L), class = "data.frame")` – Circuit Developer Jan 03 '22 at 10:31
  • @QHarr yes it's supposed to be POSIXct that's why I've used **as.POSIXct** function in the parameter. – Circuit Developer Jan 03 '22 at 10:35
  • That does look like a POSIX time, corresponding to 23 December 2021 4:30:00 AM, so it looks like there's confusion between the column name and its value(s)? – Jiří Baum Jan 03 '22 at 11:03
  • No. I was saying the you need to pass the column name where you are currently passing a transformation (the headers definition as I pulled from the docs). The transformation to the correct datatype should be done to the data before reading in as that is the datatype expected. Instead, you are getting a unix timestamp for the "2021-12-23 ...." attempting to be interpreted as a column header i.e. the data within the column. – QHarr Jan 03 '22 at 12:35

2 Answers2

0

The problem is with your time argument. The format of time in your dataset and the one you are specifying in your code do not match. That's why you are getting an error. In case you are using excel, it formats timestamps to its own default. You'll need to change it first (if it's the case).

This is what it does: enter image description here So, please check the format in your csv and what you are specifying in your code. You can change the format in excel by selecting the timestamp values and pressing Ctrl + 1 key.

enter image description here

All you need is this:

raw_data$timestamps <- as.POSIXct(raw_data$timestamps, format = "%Y-%m-%d %H:%M", tz = "UTC")

m <- df2move(raw_data, proj = "+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs", x = "x1", y = "x2", time = "timestamps", track_id = "trackId")

Vishal A.
  • 1,373
  • 8
  • 19
  • When I run the first line `raw_data$timestamps <- as.POSIXct(raw_data$timestamps, format = "%d/%m/%Y %H:%M", tz = "UTC")` it's showing **NA** in timestamps and then on running the df2move part it's giving `Error in validityMethod(as(object, superClass)) : There are NA timestamps records` – Circuit Developer Jan 03 '22 at 10:38
  • You need to remove the `NA` records for that matter. – Vishal A. Jan 03 '22 at 10:56
  • The used format of the timestamp in as.POSIXct is not adequate, so NAs are introduced. It should be: `raw_data$timestamps <- as.POSIXct(raw_data$time, format = "%Y-%m-%d %H:%M:%S", tz = "UTC")` – JKupzig Jan 03 '22 at 11:02
  • I see the problem now. Since you said, you have data in `csv`, I saved it using a `.csv` format. But what excel does is, format the timestamps, to its own default. You need to change it if it is the same case for you. – Vishal A. Jan 03 '22 at 11:03
  • I've edited my answer now. – Vishal A. Jan 03 '22 at 11:08
  • guys I removed the tz parameter as it was not provided in this answer https://stackoverflow.com/a/26280480/17820515 and made sure to convert the date before passing it in the parameter as you guys mentioned and it worked. I don't know if it's correct to remove the parameter but it's working – Circuit Developer Jan 03 '22 at 11:19
  • If it solved your problem, please consider accepting it as an answer by clicking on the grey tick mark on the left of this answer and giving an upvote. – Vishal A. Jan 03 '22 at 11:38
0

You have to specify a "character" for time within the df2move-function. Therefore, you have to do the transformation before applying the function (as @Vishal A. suggested as well). However, the transformation to Timestamps of class POSIXct was not correct, so NAs were introduced. See the solution:

raw_data <- structure(list(trackId = c("vipin", "vipin"), x = c(72.8409492130316,  72.8363572715711), y = c(18.9968003664781, 18.9958569245008),      time = c("2021-12-23 10:00:00", "2021-12-23 10:02:00"), x1 = c(72.8409492130316,      72.8363572715711), x2 = c(18.9968003664781, 18.9958569245008     ), optional = c(FALSE, FALSE), sensor = c("unknown", "unknown"     ), timestamps = structure(c(NA_real_, NA_real_), class = c("POSIXct",      "POSIXt"), tzone = "UTC")), row.names = c(NA, -2L), class = "data.frame")

raw_data$timestamps <- as.POSIXct(raw_data$time, format = "%Y-%m-%d %H:%M:%S", tz = "UTC")

m <- moveVis::df2move(raw_data, proj = "+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs", x = "x1", y = "x2", time = "timestamps", track_id = "trackId")
JKupzig
  • 1,226
  • 3
  • 13