Showing posts with label datetime. Show all posts
Showing posts with label datetime. Show all posts

Tuesday, December 24, 2013

Google Location Data

Using this blog post as my guide, I downloaded the location data that Google stores about me and plotted it via R: http://seasci.wordpress.com/2013/12/20/it-knows-where-i-live/

The json file downloaded from Google Takeout was 123 MBs, over 780k location records. It takes several minutes for the script to parse all those records.

The blog author is from UK so I needed to adjust map parameters to my relevant locations.

This script uses get_map() a lot. Here's the documentation for get_map(): http://www.inside-r.org/packages/cran/ggmap/docs/get_map

I also tried to convert the json file to a csv prior to importing into R using this py script: https://github.com/Scarygami/location-history-json-converter/blob/master/location_history_json_converter.py
I modified the csv output format a bit to include accuracy data and to add a comma between lat and lon. I think because there were some records without accuracy, I was getting errors so applied a Catch KeyError function to the accuracy line. This helped me: http://stackoverflow.com/questions/16154032/catch-keyerror-in-python

Next I need to modify my R script to read in csv instead of json.




Tuesday, December 17, 2013

R - POSIXct vs. POSIXlt



As I continue to learn about managing date and time data, I'm working to understand the difference between

POSIXct and POSIXlt

POSIXct is a date-time data object that stores the number of seconds since a certain point in time in the past.

POSIXlt stores a list of day, month, year, hour, minute, second, etc.

I'm still learning when best to use each type in different contexts. More to come on that.

R - strftime() vs. strptime()

I'm learning how to handle date and time data in R.

There are two functions that are similar when converting date/time related data.

strptime() takes a character vector (string) and converts to a POSIXlt or POSIXct data object.

strftime() takes a a POSIXlt or POSIXct data object and converts to a character vector (string).

Here is documentation on use including formatting characters: http://www.inside-r.org/r-doc/base/strftime

Saturday, December 14, 2013

R - Lubridate - Reading Date/Time Data

I'm reading in date and time data from my sleep project using the parse_date_time() function from the lubridate package.

sleep$StartDateTime <- parse_date_time(paste(sleep$Date, sleep$StartTime), "%m-%d-%Y %H.%M", truncate = 2)

The date and time data were in two separate columns so I needed to use the paste()function. The minutes data is in the format HH.MM.

Also, I was getting NA data at first when the minutes data was 00 (at the top of the hour). I added the truncate argument so that it would ignore incomplete data in seconds and minutes.

Here is a good summary of the parse_date_time() function: http://www.inside-r.org/packages/cran/lubridate/docs/parse_date_time

Monday, December 9, 2013

R - Converting Column to Date

In my sleep data, I have a date column that was originally a character type. When plotted in ggplot(), it was listing every date in the x axis, making the labels very crowded.

Then I added the following code

dailytotals$EndDate <- as.Date(dailytotals$EndDate , "%m-%d-%Y")

which converted it to a date type. Then ggplot automatically created tick marks and labels every 15 days (for data that spans two months.

Note that the %Y (capitalized) recognizes a four digit year where %y (lower case) recognizes a two digit year.