
# Loading data for exploratory analysis


```{r, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, cach=FALSE,warning = FALSE,message = FALSE)
```


```{r, results='hide',message=FALSE}

library(tidyverse)
library(aqm)
library(readr)
```


## Introduction

There are three elements to analysing any data set. 

1. Exploratory data analysis
2. Statistical model fitting
3. Presenting the final results

These elements do overlap to some extent in any work flow. There is no clear boundary between exploratory analysis and statistical modelling, as statistical modelling itself is often used for exploration. However you will tend to find yourself working through these phases on the path to writing up any analysis.

Exploratory, descriptive data analysis is a vital first step.  We will look at the data on sleep in mammals [@Allison1976]. The data frame has been slightly modified to include a column on diet.

### Video 1:Picking up where we left off

[![Video 1](https://img.icons8.com/nolan/64/video.png)](http://r.bournemouth.ac.uk:82/short_courses/Intro_to_RStudio/videos/sleep_1.mov "Link Title")

### Video 2:Starting a new markdown document

[![Video 1](https://img.icons8.com/nolan/64/video.png)](http://r.bournemouth.ac.uk:82/short_courses/Intro_to_RStudio/videos/sleep_2.mov "Link Title")

## Reproducible research

The great advantage in using R code for data analysis as opposed to a graphical user interface approach is reproducibility and transferability. The documents you are provided with on the course have all been written in markdown and compiled on the server. So the code has been tested and will produce the results shown. In order to produce the same results you do not need to remember any R at all. Carefully copying and pasting the code from the instructions document into the document you are working on yourself will produce the same results when the markdown is compiled. 

### Video 3:Reproducible research: Reusing code

[![Video 1](https://img.icons8.com/nolan/64/video.png)](http://r.bournemouth.ac.uk:82/short_courses/Intro_to_RStudio/videos/sleep_3.mov "Link Title")

### Video 4: Loading packages

When we are using R we almost always load packages before starting work. A package bundles together code, data, documentation, and tests, and is easy to share with others. There are over 20,000 packages available on the Comprehensive R Archive Network, or CRAN, the public clearing house for R packages. 
This variety of packages is one of the reasons that R is so successful: the chances are that someone has already solved a problem that you’re working on, and you can benefit from their work by downloading their package.

[![Video 1](https://img.icons8.com/nolan/64/video.png)](http://r.bournemouth.ac.uk:82/short_courses/Intro_to_RStudio/videos/sleep_4.mov "Link Title")



### Video 5: Editing opts_chunk$set

A markdown document usually starts with a hidden code chunk that sets some options. This can be carefully edited to prevent chunks displaying unwanted messages and even to switch the visibility of the code on and off.

[![Video 1](https://img.icons8.com/nolan/64/video.png)](http://r.bournemouth.ac.uk:82/short_courses/Intro_to_RStudio/videos/sleep_5.mov "Link Title")

Please be very careful if you edit this chunk. Any error will prevent the document from knitting.

## Loading in the data

Let's load our data back into R

```{r}
d<-read_csv("sleep.csv")
```

I've called the data frame "d" here. This makes typing faster and is accepted practice if the analysis is only based on a single data frame. If an analysis uses several tables of data at the same time then it is better to use informative names for each one.

A data frame is a table containing multiple columns. Each column is a variable. Columns containing names are categorical variables and will usually be referred to as factors in R. 

### Video 6: Loading data and running code chunks in order

[![Video 1](https://img.icons8.com/nolan/64/video.png)](http://r.bournemouth.ac.uk:82/short_courses/Intro_to_RStudio/videos/sleep_6.mov "Link Title")

### Video 7: Looking at the data. 

When data are loaded into R they are held in memory. You can see data objects in the environment pane in the top right of the interface. Clicking on a data object brings up a spreadsheet like table of the data that you can look through, search and sort. Unlike working with a spreadsheet you can't change the data. That is not the R way of working. You make changes to the data in R by typing in code. This is simply a handy way of checking your data. As the columns can be sorted by clicking on the top row it is easy to find the maximum and minimum values.

[![Video 2](https://img.icons8.com/nolan/64/video.png)](http://r.bournemouth.ac.uk:82/short_courses/Intro_to_RStudio/videos/sleep_7.mov "Link Title")

### The structure of the data frame

There are two ways of quickly understanding the data. The simplest is just to click on the data frame in the environment pane. This pops up the data in the form of spreadsheet like table which shows on the top left tab. When you have finished looking at the data, close down the tab.

The other way of finding out information about the data is to ask R directly.
The str command produces information about the structure.

```{r}
str(d)
```


A data frame is the most commonly used data object in R. Whenever you load in data from a spreadsheet into R you get a data frame. A data frame is a table with rows and columns. Each column corresponds to a variable.  The str command provides information on the nature of the variable. 

The summary command is very handy if you want to find the summary statistics for all the variables at once. These statistics include the maximum value, minimum value, mean, median and the interquartile ranges.

```{r}
summary(d)
```



### Video 8: Talking about data exploration

[![Video 3](https://img.icons8.com/nolan/64/video.png)](http://r.bournemouth.ac.uk:82/short_courses/Intro_to_RStudio/videos/sleep_8.mov "Link Title")


