2018年2月9日金曜日

manage blood pressure part.2


# read data as csv format and convert to xts to plot
# bp <- read.csv("~/bp.csv")
bp <- read.csv("~/Downloads/bp - シート1.csv")
#

# create xts, remove data and time from the original and
# append POSIX time stamp.
bp.xts <- xts(bp[,c(-1,-2)],as.POSIXct(paste(bp$Date,bp$Time,sep=" ")))
# calculate weekly mean except the data of the sample whose $High is <=100
#
apply.weekly(bp.xts[bp.xts$High > 101],mean)
# drow plot with standard line at 85 and 130.

plot(merge(bp.xts,cbind(rep(85,length(bp.xts[,1])),rep(130,length(bp.xts[,1])))),col = c("red", "blue","green","green"),lwd=c(3,3,2,2),major.ticks='days',grid.ticks.on='days')

# or exclude the data $High is <= 100

plot(merge(bp.xts[,-3][bp.xts$High > 101],cbind(rep(85,length(bp.xts[,1][bp.xts$High > 101])),rep(130,length(bp.xts[,1][bp.xts$High > 101])))),col = c("red", "blue","green","green"),lwd=c(3,3,2,2),major.ticks='days',grid.ticks.on='days')


plot(xts(bp[,c(-1,-2,-5)],as.POSIXct(paste(bp$Date,bp$Time,sep=" "))))



# calculate 4 days moving average

 plot(last(index(xts(bp[,c(-1,-2,-5)],as.POSIXct(paste(bp$Date,bp$Time,sep=" ")))),n=length(na.omit(filter(bp$High,rep(1,4))/4))),na.omit(filter(bp$High,rep(1,4))/4),type='l')

# same but for 7 days.

 plot(last(index(xts(bp[,c(-1,-2,-5)],as.POSIXct(paste(bp$Date,bp$Time,sep=" ")))),n=length(na.omit(filter(bp$High,rep(1,7))/7))),na.omit(filter(bp$High,rep(1,7))/7),type='l')


# exclude the data whose High is more than 150 and less than 101 and calculate
# mean value.
mean(na.omit(bp.xts[bp.xts$High < 150 & bp.xts$High > 101][,1]))

# 1) draw High and Low by red and blue each
# 2) define linewidth for High, Low and rep(130,74)
# 3) draw an additional line at 130
# 4) set x lim tick frequency to "days"

plot(merge(bp.xts,rep(130,74)),col = c("red", "blue","black"),lwd=c(3,3,0.5),major.ticks='days')

# 6) and add grid frequency to "days"

plot(merge(bp.xts,rep(130,74)),col = c("red", "blue","green"),lwd=c(3,3,0.5),major.ticks='days',grid.ticks.on='days')

plot(merge(bp.xts,cbind(rep(85,length(bp.xts[,1])),rep(130,length(bp.xts[,1])))),col = c("red", "blue","green","green"),lwd=c(3,3,2,2),major.ticks='days',grid.ticks.on='days')

0 件のコメント: