# [R] How does one set up logical functions?

Gabor Grothendieck ggrothendieck at gmail.com
Tue May 7 18:42:05 CEST 2013

```On Tue, May 7, 2013 at 11:06 AM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> On Tue, May 7, 2013 at 10:02 AM, Neotropical bat risk assessments
> <neotropical.bats at gmail.com> wrote:
>> Hi all,
>>
>> I am trying to set up logical function(s) to deal with two adjustments
>> to a blood glucose value.
>> I have been dinking around in Excel and assume this will be much easier
>> in R.
>>
>> DF is date-time, BG value in mg/dL,test strip
>> 4/3/13 19:20    105     Aviva-491350
>> 4/4/13 21:03    74      Aviva-491350
>> 4/6/13 17:40    81      Aviva-491640
>> 4/6/13 17:40    82      Aviva-491350
>> 4/6/13 22:48    106     Aviva-491640
>> 4/6/13 22:48    102     Aviva-491350
>> 4/7/13 5:32     87      Aviva-491350
>> 4/7/13 5:32     103     Aviva-491640
>>
>>
>> What I need are the high and low ranges based on "acceptable" standards
>> of the measured values.
>>
>> The logical expressions need to be
>> IF BG =>100 then "High limit" would = (BG+(BG*.15))
>> IF BG =>100 then "Low limit" would = (BG-(BG*.15))
>> and
>> IF BG <100 then "High limit" would = (BG+15)
>> IF BG <100 then "Low limit" would = (BG-15)
>>
>> The standards are written as: 95% of the individual glucose results
>> shall fall within ą15 mg/dL of the reference results at glucose
>> concentrations less than 100 mg/dL and within ą15% at glucose
>> concentrations greater than or equal to 100 mg/dL.
>>
>> Then I need to plot the measured value and also show the high & low
>> "acceptable" values.
>>
>
> Here it is using gglot2:
>

Here it is again with some fixes and also reading the input data so
its all self contained:

library(ggplot2)
library(gridExtra)

Lines <- "date time BG test_strip
4/3/13 19:20  105     Aviva-491350
4/4/13 21:03   74      Aviva-491350
4/6/13 17:40   81      Aviva-491640
4/6/13 17:40   82      Aviva-491350
4/6/13 22:48   106     Aviva-491640
4/6/13 22:48   102     Aviva-491350
4/7/13 5:32    87      Aviva-491350
4/7/13 5:32    103     Aviva-491640"

DF2 <- transform(DF,
datetime = as.POSIXct(paste(date, time), format = "%m/%d/%y %H:%M"),
lower = ifelse(BG < 100, BG - 15, BG * 0.85),
upper = ifelse(BG < 100, BG + 15, BG * 1.15))

ggplot(DF2, aes(datetime, BG)) +
geom_point() +
geom_line() +
geom_smooth(aes(ymin = lower, ymax = upper), stat = "identity") +
geom_linerange(aes(ymin = lower, ymax = upper)) +
annotation_custom(tableGrob(DF2, gp = gpar(cex = 0.5)), ymin = 120) +
coord_cartesian(ylim = c(60, 150)) +
xlab("") +
ylab("Blood Glucose") +
ggtitle("Blood Glucose Levels")

--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

```