agrignard / TARA

0 stars 0 forks source link

Cleaning and Formatting data with R #3

Open ooookai opened 5 years ago

ooookai commented 5 years ago

Steps

  1. Merge 21 GPS raw data files ( 20181006 - 20181026 )
  2. Prepare Formatted data for Map

Output


R

load dataset

setwd("~/TARA/includes/GPS_Postion/raw_BostonLorient")
filenames <- list.files(full.names = T)
All <- lapply(filenames,function(i){
    read.csv(i, header=FALSE)
})
df <- do.call(rbind.data.frame, All)
names(df) <- c("t1", "c1", "s", "t2", "lat", "lng", "hms", "dmy")
View(df)
write.csv(df, file = "tara_tsg_20181006-20181026.csv", row.names=FALSE)

define functions

latlngSign <- function(text){
    result <- ifelse(is.na(str_match(text, "[WS]")), "", "-")
}

extractNum <- function(text) {result <- str_extract(text, "[0-9]+.?[0-9]+")}

latlngNum <- function(text) {
    result <- paste(sep = '', latlngSign(text), extractNum(gsub(text, pattern = "([0-9]+) ([0-9]+).([0-9]+)", replacement = "\\1.\\2\\3")))
}

hmsdmy2time = function(hms, dmy) {
    hmsdmy = paste(hms, dmy, sep = "-")
    secs = as.numeric(as.POSIXct(hmsdmy, tz ="UTC" ,format = "%H%M%S-%d%m%y"))
    result <- as.character(secs * 1000)
}

transform data.frame

dff = transform(df, 
          t1  = extractNum(t1),
          t2  = extractNum(t2),
          c1  = extractNum(c1),
          s   = extractNum(s),
          lat = latlngNum(lat),
          lng = latlngNum(lng),
          hms = extractNum(hms),
          dmy = extractNum(dmy),
          time = hmsdmy2time(extractNum(hms), extractNum(dmy))
)

write.csv(dff, file = "tara_tsg_20181006-20181026_Cleaning.csv", row.names=FALSE)
ooookai commented 5 years ago

Preview data on CartoDB map.

link: https://ooookai.carto.com/builder/8f05083b-3596-456d-a4cf-9c46438a51e1/embed


image

agrignard commented 5 years ago

@ooookai really nice starting point!!!

I have check his link https://ooookai.carto.com/builder/8f05083b-3596-456d-a4cf-9c46438a51e1/embed

However the boat desappear in the middle of the trup from 12 to 20th of October