In the last session we saw the RStudio interface and started typing at the R command prompt to practice basic R calculations and variable setting. Then we saw how to read files into R.
As a review: Here is how we were reading our data files last time:
Where are these files now?
To use data stored on a computer we need to tell R where it is. This is done using paths.
Paths can be absolute:
#cars <- read.csv('/home/rharbert/GitHub/bioinformatics/docs/mtcars.csv') #Linux path type for server
#cars <- read.csv('/Users/rharbert/.../mtcars.csv') #Windows type if working on a personal machine
Your paths will be different
Folders/Directories are separate by / with the file name at the end.
Paths can also be relative:
“From where I am open the mtcars file”
How do you figure out what the path should be?
## [1] "C:/Users/rharbert/Documents/GitHub/bioinformatics/docs"
Prints the current path of your R session.
If you need to change the directory you are working in, use:
One of R’s biggest advantages is the ability to create high quality graphics in nearly any format or style. Today we will be working with the basic plotting features of R. These are good, but limited at times. Later we will take a look at the ggplot library. ggplot is the current ‘state of the art’ in graphics for R.
OK. That was not so great. Let’s try somethnig more useful for visualizing these data. We can tell plot() which columns we want to create a scatterplot for:
## [1] "X" "model" "mpg" "cyl" "disp" "hp" "drat" "wt"
## [9] "qsec" "vs" "am" "gear" "carb"
OR we can create other types of plots by calling other functions. e.g., a histogram of boxplot:
Programming is just getting your computer to do the same thing a bunch of times so you don’t have to.
The fundamental structure in any programming language to make this happen is the Loop. We will look at loops to understand the process.
Repeating tasks using loops
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
Catch loop output in a vector or list
The Apply functions in R provide efficient repetition that usually out-performs for loops.
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 1 1 1 1
## [2,] 2 2 2 2 2
## [3,] 3 3 3 3 3
## [4,] 4 4 4 4 4
## [5,] 5 5 5 5 5