# R notes for Activity 19-5: Sleeping Times

I will gather your sleep time data at the start of class and enter them into a file. You can read them into a table with

```SleepData = read.csv("/home/rebelsky/Stats115/Data/SleepData.csv")
```

The second line allows us to refer to the vector as `SleepTimes`.

## 19-5 a: Graphical Displays

You should refer to activity 19-3 c for ideas of graphical displays.

## 19-5 b: Sample Statistics

You can get sample size, sample mean, and sample standard deviation with

```length(SleepTimes)
mean(SleepTimes)
sd(SleepTimes)
```

## 19.5 d: Confidence Interval

Since we have all of the original data, the easiest way to get the confidence interval is to use the `t.test` function. You should substitute your own guess as to the mean hours slept (in place of 6).

```t.test(SleepTimes, mu=6, conf.level=.90)
```

Of course, you might also want to provide R with step-by-step instructions.

```x_bar = mean(SleepTimes)
n = length(SleepTimes)
s = sd(SleepTimes)
t_star = qt(0.95, n-1)
ci_lower = x_bar - t_star*s/sqrt(n)
ci_upper = x_bar + t_star*s/sqrt(n)
c(ci_lower, ci_upper)
```

## 19-5 e: Counting

Rather than counting values by hand, you can get R to produce a vector of the times in this interval with

```NearMedian = SleepTimes[(SleepTimes > ci_lower) & (SleepTimes < ci_upper)]
length(NearMedian)
```

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright (c) 2007-8 Samuel A. Rebelsky.

This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit `http://creativecommons.org/licenses/by-nc/2.5/` or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.