|
library(lubridate) |
|
library(ggplot2) |
|
library(zoo) |
|
|
|
con <- file("~/Dropbox/Text Notes/Weight.txt", "rt") |
|
lines <- readLines(con) |
|
close(con) |
|
|
|
parse.line <- function(line) { |
|
s <- strsplit(line, split=" ")[[1]] |
|
date.str <- paste(s[2:10][!is.na(s[2:10])], collapse=" ") |
|
date <- mdy_hm(date.str, quiet=TRUE) |
|
list(s[1], date) |
|
} |
|
|
|
list.weight.date <- lapply(lines, parse.line) |
|
weights= lapply(list.weight.date, function(X) as.numeric(X[1])) |
|
dates = lapply(list.weight.date, function(X) X[[2]]) |
|
|
|
df <- data.frame(weight = unlist(weights), date = do.call("c", dates) ) |
|
|
|
ts <- zoo(c(df$weight), df$date) |
|
ts <- aggregate(ts, time(ts), tail, 1) |
|
g <- round(seq(start(ts), end(ts), 60 * 60 * 24), "days") |
|
ts <- na.approx(ts, xout = g) |
|
|
|
days.ago <- function(days, smooth.n) { |
|
date <- head(tail(index(ts),days + 1),1) |
|
smoothed <- rollmean(ts, smooth.n, align="right") |
|
as.numeric(smoothed[date]) |
|
} |
|
|
|
current.weight <- days.ago(0, 10) |
|
x <- c(current.weight, days.ago(7, 10)-current.weight, days.ago(30, 10)-current.weight, days.ago(365, 10)-current.weight,max(ts)-current.weight) |
|
|
|
cat(paste("Weight (lbs):", round(x[1], 1),"\n")) |
|
cat(paste("1 Week Δ:", round(x[2], 1),"\n")) |
|
cat(paste("1 Month Δ:", round(x[3], 1),"\n")) |
|
cat(paste("1 Year Δ:", round(x[4], 1),"\n")) |
|
cat(paste("Total Δ:", round(x[5], 1),"\n")) |