Goals:- Become comfortable with development in R.
- Understand how to use vectorized operations to solve problems.
- Start to look at relationships between variables.
Outline:- Introduce documentation facilities in R (using spin and knitr).
- Look at data frames, factors, and logical indexing for operations.
- Start to look at relationships between variables and modeling
Resources:
Some R preliminaries:- Install knitr for document layout
- Use knitr
library('knitr')
- Install ggplot2 for better graphics
- You should set your R Studio so that the package viewer is visible. That way you can see what is currently installed.
Example 1: Turn your script into an html file by first creating a markdown version using spin. Then knit the markdown to create html. Suppose thescript in the current directory is called lab1.R. Just type:
- commentary: #'
- chunk control: #+
- inline code: {{ }}
NOTE: For a direct translation with no markdown translation you can select from the File menu of RStudio when you are editing a .R script.Compile NotebookExample 2: Look at the Rmd script from Lecture 6.Using factors and vector logic:Example 3: A data frame with factor data: data(Loblolly)- Look at the data in the View
- Read the help for the data
- Examine the data attributes
Example 4: Pick out the entries of Loblolly corresponding to Seed type 305 and plot.Example 5: Pick out the entries of Loblolly which have a tree age of at least 10.Example 6: The example for Loblolly uses the plot.formula form of plotting with subsettingRelationships between variables:Vector variables x and y are linearly related if y_{i} = m * x_{i} + b (When plotted against each other as ordered pairs the points fall on a line.)Covariance measures how linearly related two variables are: cov(x, y)Correlation is a normalized measure of how linearly related variables are: cor(x, y). The values of correlation are between -1 and 1. A value of zero indicates no relationship. |