⌘+C
Warning: package 'dplyr' was built under R version 4.2.3
Warning: package 'stringr' was built under R version 4.2.3
⌘+C
Warning: package 'scales' was built under R version 4.2.3
Jihong Zhang
July 6, 2021
The data contained within Health app on iPhone can be saved and exported for other uses. Perhaps you want to export Health app data to use in another health or fitness app, importing it to elsewhere, or maybe you want to use the raw Health data for your own purposes.
Exporting Health data from iPhone results in a zip archive that contains the raw data as gathered by Health app in XML format. This exported Health data will include any data stored or gathered by the Health app and any associated devices, including any Medical ID data, the native iPhone step counter and distance tracker, any data from an Apple Watch, and any data gathered from any third party devices that are syncing to Health app, like a smart scale or blood pressure monitor.
You can find more details about exporting data from How to Export Health Data form iPhone and How to Export, Parse and Explore Your Apple Health Data with Python.
Warning: package 'dplyr' was built under R version 4.2.3
Warning: package 'stringr' was built under R version 4.2.3
Warning: package 'scales' was built under R version 4.2.3
The .xml file could be downloaded and exported directly from iphone’s health app. To read in the XML file and transform it into data frame, XML
R package will help.
rc <- readRDS("export_health_care.rds")
## Filter useful data
apple_watch <- rc %>% filter(grepl("JZ",sourceName), unit == 'count/min',
type == "HKQuantityTypeIdentifierRestingHeartRate")
# Adjusting to Local Timezone
apple_watch_dt <- apple_watch %>%
mutate(cdt=as_datetime(creationDate, tz="US/Central"),
stm=as_datetime(startDate, tz="US/Central"),
etm=as_datetime(endDate, tz="US/Central"),
dst=as.numeric(as.character(value)))
# %>%
# group_by(creationDate) %>%
# mutate(TotalTime=cumsum(value)) %>% # cumulative distance covered #
# mutate(hr=hour(stm), min=minute(stm)) %>%
# mutate(elt=as.numeric(etm-mntm)) %>% # total elapsed time #
# mutate(dtm=as.numeric(etm-lag(etm))) %>%
# ungroup()
sourceName | unit | cdt | stm | etm | dst |
---|---|---|---|---|---|
JZ’sApple Watch | count/min | 2020-07-27 22:42:32 | 2020-07-27 08:45:05 | 2020-07-27 22:37:26 | 60 |
JZ’sApple Watch | count/min | 2020-07-28 19:43:28 | 2020-07-28 00:03:04 | 2020-07-28 19:40:28 | 59 |
JZ’sApple Watch | count/min | 2020-07-29 14:22:15 | 2020-07-29 08:40:16 | 2020-07-29 14:18:28 | 54 |
JZ’sApple Watch | count/min | 2020-07-30 17:44:05 | 2020-07-30 09:06:24 | 2020-07-30 17:41:27 | 54 |
JZ’sApple Watch | count/min | 2020-07-31 17:36:00 | 2020-07-31 08:55:46 | 2020-07-31 17:27:30 | 52 |
JZ’sApple Watch | count/min | 2020-08-01 12:17:02 | 2020-08-01 00:03:31 | 2020-08-01 12:14:41 | 60 |
There a decent blog illustrating how to handle with apple watch export file.
---
title: Health Care Data Analysis with Apple Watch
author: 'Jihong Zhang'
date: '2021-07-06'
draft: false
private: true
slug: health-care-data-analysis-with-apple-watch
categories:
- blog
- R
- health
summary: "Analyze the health care data from Apple Watch's app"
lastmod: '2021-07-06T14:02:09-05:00'
featured: no
---
> The data contained within Health app on iPhone can be saved and exported for other uses. Perhaps you want to export Health app data to use in another health or fitness app, importing it to elsewhere, or maybe you want to use the raw Health data for your own purposes.
>
> Exporting Health data from iPhone results in a zip archive that contains the raw data as gathered by Health app in XML format. This exported Health data will include any data stored or gathered by the Health app and any associated devices, including any Medical ID data, the native iPhone step counter and distance tracker, any data from an Apple Watch, and any data gathered from any third party devices that are syncing to Health app, like a smart scale or blood pressure monitor.
You can find more details about exporting data from [How to Export Health Data form iPhone](https://osxdaily.com/2019/05/20/export-health-data-from-iphone/) and [How to Export, Parse and Explore Your Apple Health Data with Python](http://www.markwk.com/data-analysis-for-apple-health.html).
## Load Required Packages
```{r setup, comment=FALSE, echo=FALSE, results="hide", comment=FALSE}
knitr::opts_chunk$set(
echo = TRUE,
result = 'hide',
eval = FALSE,
message=FALSE # suppress message from packages and warning
)
library(knitr)
```
```{r, eval=TRUE}
rm(list = ls(all = TRUE))
if(!require(XML)) install.packages("XML")
if(!require(tidyverse)) install.packages("tidyverse")
if(!require(lubridate)) install.packages("lubridate")
if(!require(scales)) install.packages("scales")
if(!require(ggthemes)) remotes::install_github(c("hadley/ggplot2", "jrnold/ggthemes"))
if(!require(ggridges)) remotes::install_github("wilkelab/ggridges")
if(!require(rpart)) install.packages("rpart")
if(!require(kableExtra)) install.packages("kableExtra")
```
## Read in XML data
The .xml file could be downloaded and exported directly from iphone's health app. To read in the XML file and transform it into data frame, `XML` R package will help.
```{r, eval=FALSE}
loc<-"导出.xml" # enter file path location of export.xml file here #
xml <- xmlParse(loc)
rc <- data.frame(XML:::xmlAttrsToDataFrame(xml["//Record"]), stringsAsFactors = F)
saveRDS(rc, "export_health_care.rds")
```
```{r, eval=TRUE}
rc <- readRDS("export_health_care.rds")
## Filter useful data
apple_watch <- rc %>% filter(grepl("JZ",sourceName), unit == 'count/min',
type == "HKQuantityTypeIdentifierRestingHeartRate")
# Adjusting to Local Timezone
apple_watch_dt <- apple_watch %>%
mutate(cdt=as_datetime(creationDate, tz="US/Central"),
stm=as_datetime(startDate, tz="US/Central"),
etm=as_datetime(endDate, tz="US/Central"),
dst=as.numeric(as.character(value)))
# %>%
# group_by(creationDate) %>%
# mutate(TotalTime=cumsum(value)) %>% # cumulative distance covered #
# mutate(hr=hour(stm), min=minute(stm)) %>%
# mutate(elt=as.numeric(etm-mntm)) %>% # total elapsed time #
# mutate(dtm=as.numeric(etm-lag(etm))) %>%
# ungroup()
```
```{r, eval=TRUE, results='asis'}
head(apple_watch_dt[,-1]) %>%
select(sourceName, unit, cdt, stm, etm, dst) %>%
kbl() %>%
kable_material_dark(full_width = F, html_font = "Maven Pro") |>
kable_styling(font_size = 10)
```
## Other materials
There a decent [blog](https://www.inpredictable.com/2021/06/analyzing-your-run-data-with-r.html) illustrating how to handle with apple watch export file.