Skip to content

Instantly share code, notes, and snippets.

@dill
Created January 5, 2018 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dill/7a892d31ecc68c29ca3af0eea2d5aebd to your computer and use it in GitHub Desktop.
Save dill/7a892d31ecc68c29ca3af0eea2d5aebd to your computer and use it in GitHub Desktop.
Do a quick analysis of the Health data collected by iPhones
library(XML)
# pull in the xml file
# can export this from the iPhone and e-mail it to youself
xx <- xmlParse("export.xml")
xx <- xmlToList(xx)
# now do some data mudging
library(plyr)
# grab only step counts
gg <- ldply(xx, function(x){
if(!("type" %in% names(x))){
return(NULL)
}
if(x[["type"]]=="HKQuantityTypeIdentifierStepCount"){
return(x)
}else{
return(NULL)
}
})
# remove some useless columns
gg$device <- gg$.id <- gg$type <- gg$sourceName <- gg$sourceVersion <-
gg$unit <- NULL
# make the dates make sense
# not sure if this is right because it looks like everything is
# recorded in UTC, but I definitely wasn't in UTC all year...
library(lubridate)
gg$ymd <- ymd_hms(gg$startDate)
# just get 2017 data
gg <- gg[year(gg$ymd)==2017, ]
# truncate to days
gg$day <- as.character(trunc(gg$ymd, "days"))
gg$value <- as.numeric(gg$value)
# get steps/day
dd <- ddply(gg, .(day), summarize, steps=sum(value))
# shortcuts
dd$day <- ymd(dd$day)
dd$wday <- wday(dd$day, week_start=1, abbr=FALSE, label=TRUE)
dd$month <- month(dd$day, abbr=FALSE, label=TRUE)
# now do some plotting
library(ggplot2)
g <- ggplot(dd) +
geom_col(aes(x=day, y=steps)) +
geom_hline(yintercept=10000, colour="red") +
theme_minimal() +
labs(x="Date", y="Steps")
g_day <- ggplot(dd) +
geom_boxplot(aes(x=wday, group=wday, y=steps)) +
geom_hline(yintercept=10000, colour="red") +
theme_minimal() +
labs(x="Week day", y="Steps")
g_month <- ggplot(dd) +
geom_boxplot(aes(x=month, group=month, y=steps)) +
geom_hline(yintercept=10000, colour="red") +
theme_minimal() +
labs(x="Week day", y="Steps")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment