Home > Back-end >  Error: Column named '1640233800' cannot be found in 'df'
Error: Column named '1640233800' cannot be found in 'df'

Time:01-03

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'

CodePudding user response:

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")

CodePudding user response:

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")

  •  Tags:  
  • r
  • Related