---
title: "BiCG R Notebook"
output: html_notebook
---

### Getting Around

#### The Hard Way

```{r}
# get the current working directory:
getwd()
# set a new working directory:
setwd("C:/myPATH")
setwd("~/myPATH") # on Mac
setwd("/Users/david/myPATH") # on Mac
# list the files in the current working directory:
list.files()
# list objects in the current R session:
ls()
```

#### The Easy Way

In RStudio we can use "Session" > "Set Working Directory".  
  
### Data Types

#### Vectors

```{r}
numeric.vector <- c(1,2,3,4,5,6,2,1)
numeric.vector

character.vector <- c("Fred", "Barney", "Wilma", "Betty")
character.vector

logical.vector <- c(TRUE, TRUE, FALSE, TRUE)
logical.vector
```

To refer to elements in the vector:

```{r}
character.vector
character.vector[2]
character.vector[2:3]
character.vector[c(2,4)]
```

#### Matrices

You can create a 3x4 numeric matrix with:

```{r}
matrix.example <- matrix(1:12, nrow = 3, ncol=4, byrow = FALSE)
matrix.example

matrix.example <- matrix(1:12, nrow = 3, ncol=4, byrow = TRUE)
matrix.example
```

Alternatively, you can create a matrix by combining vectors:

```{r}
dataset.a <- c(1,22,3,4,5)
dataset.b <- c(10,11,13,14,15)
dataset.a
dataset.b

rbind.together <- rbind(dataset.a, dataset.b)
rbind.together

cbind.together <- cbind(dataset.a, dataset.b)
cbind.together
```
To get elements of the matrix:

```{r}
matrix.example[2,4]
matrix.example[2,]
matrix.example[,4]
```

You can add column and row names to the matrix and use the new names to get the elements of the matrix:

```{r}
colnames(matrix.example) <- c("Sample1","Sample2","Sample3","Sample4")
rownames(matrix.example) <- paste("gene",1:3,sep="_")
matrix.example

matrix.example[,"Sample2"]
matrix.example[1,"Sample2"]
matrix.example["gene_1","Sample2"]
```

Note that all columns in a matrix must have the same mode(numeric, character, etc.) and the same length.

#### Dataframes

Dataframes are similar to arrays but different columns can have different modes (numeric, character, factor, etc.).  

```{r}
people.summary <- data.frame(
                             age = c(30,29,25,25),
                             names = c("Fred", "Barney", "Wilma", "Betty"),
                             gender = c("m", "m", "f", "f")
                             )
people.summary
```

To get elements of the dataframe:

```{r}
people.summary[2,1]
people.summary[2,]
people.summary[,1]
people.summary$age
```

#### Lists

Lists gather together a collection of objects under one name.

```{r}
together.list <- list(
                      vector.example = dataset.a, 
                      matrix.example = matrix.example,
                      data.frame.example = people.summary
                      )
together.list
```

There are several ways to get elements of a list:

```{r}

together.list$matrix.example
together.list$matrix.example[,3]
together.list["matrix.example"]
together.list[["matrix.example"]]
together.list[["matrix.example"]][,2]
```

### Reading Data In

We use `read.data` or `read.csv` to read in data.  

```{r}
gene_example <- read_csv("Gene_R_example.csv")
```

In RStudio, we can use the "File" navigation window instead.  Navigate to the directory containing the Gene_R_example.csv that we downloaded previously. Click on the file name then click "Import Dataset."  A new window appears allowing you to modify attributes of your file.  Rename the file to the object "gene_example".

Commands like `head` and `tail` also work in R.

```{r}
head(gene_example)
View(gene_example)
```

### Basic Plotting

A very basic plot:

```{r}
plot(x=gene_example$Control, y=gene_example$Treated)
```

A nicer plot:

```{r}
plot(x=gene_example$Control, y=gene_example$Treated,
    xlab = "Control",
    ylab = "Treated",
    cex.lab = 1.5,
    main = "A nice scatter plot",
    pch = 16,
    bty = "n",
    col = "dark blue",
    las = 1
    )
## las
## How to change the axes label style in R
## To change the axes label style, use the graphics option las (label style). This changes the orientation angle of the labels:
## 0: The default, parallel to the axis
## 1: Always horizontal
## 2: Perpendicular to the axis
## 3: Always vertical

## bty
## To change the type of box round the plot area, use the option bty (box type):
## "o": The default value draws a complete rectangle around the plot.
## "n": Draws nothing around the plot.
```

Connecting the dots:

```{r}
plot(x=gene_example$Control, y=gene_example$Treated,
    xlab = "Control",
    ylab = "Treated",
    cex.lab = 1.5,
    main = "A nice scatter plot",
    pch = 16,
    bty = "n",
    col = "dark blue",
	type = "b",
	las = 1
	)
```

#### Histograms

```{r}
hist(gene_example$Control)

hist(gene_example$Control,
    xlab = "Expression",
    ylab = "Number of Genes",
    cex.lab = 1.5,
    main = "A nice histogram",
    col = "cyan",
    breaks = 10,
    las = 1
    )
```

#### Boxplots

```{r}
 boxplot(gene_example[,2:3])
```

```{r}
boxplot(gene_example[,2:3],
	width = c(3,1),
	col = "red",
	border = "dark blue",
	names = c("Control", "Treatment"),
	main = "My boxplot",
	notch = TRUE,
	horizontal = TRUE
	)
```

#### Saving your plots as PDFs

```{r}
pdf("myfigure.pdf", height=10, width=6)
par(mfrow=c(2,1))

plot(x=gene_example$Control, y=gene_example$Treated,
    xlab = "Control",
    ylab = "Treated",
    cex.lab = 1.5,
    main = "A nice scatter plot",
    pch = 16,
    bty = "n",
    col = "dark blue",
	type = "b",
	las = 1
	)

boxplot(gene_example[,2:3],
	width = c(3,1),
	col = "red",
	border = "dark blue",
	names = c("Control", "Treatment"),
	main = "My boxplot",
	notch = TRUE,
	horizontal = TRUE
	)
  
  dev.off()
 
```
   
   



