R and RStudio

R is a programming language originally designed by statisticians. Now it is a common programming language for scientific applications.

In this course we will use R through the RStudio interface. Your instructor will demonstrate how to access and log into the RStudio server for this class.

version
##                _                           
## platform       x86_64-pc-linux-gnu         
## arch           x86_64                      
## os             linux-gnu                   
## system         x86_64, linux-gnu           
## status                                     
## major          3                           
## minor          6.1                         
## year           2019                        
## month          07                          
## day            05                          
## svn rev        76782                       
## language       R                           
## version.string R version 3.6.1 (2019-07-05)
## nickname       Action of the Toes

We can now get started with the R command promp open.

x=2
print(x) ##Print method
## [1] 2
class(x)
## [1] "numeric"
x=seq(1:10) # Create a vector
class(x)
## [1] "integer"
print(x)
##  [1]  1  2  3  4  5  6  7  8  9 10
print(x[1]) # First index of vector
## [1] 1
print(x[1:5])
## [1] 1 2 3 4 5
y = matrix(nrow=5, ncol=5) # create a 5x5 matrix
print(y)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]   NA   NA   NA   NA   NA
## [2,]   NA   NA   NA   NA   NA
## [3,]   NA   NA   NA   NA   NA
## [4,]   NA   NA   NA   NA   NA
## [5,]   NA   NA   NA   NA   NA
class(y)
## [1] "matrix"
y[1,1] = 5
print(y)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    5   NA   NA   NA   NA
## [2,]   NA   NA   NA   NA   NA
## [3,]   NA   NA   NA   NA   NA
## [4,]   NA   NA   NA   NA   NA
## [5,]   NA   NA   NA   NA   NA
y[,1]= x[1:5]
print(y)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1   NA   NA   NA   NA
## [2,]    2   NA   NA   NA   NA
## [3,]    3   NA   NA   NA   NA
## [4,]    4   NA   NA   NA   NA
## [5,]    5   NA   NA   NA   NA
class(y[,1])
## [1] "numeric"
y = cbind(seq(1:5), 
seq(1:5),
seq(1:5),
seq(1:5),
seq(1:5))

class(y)
## [1] "matrix"

Read table/tab/csv/txt text files:

read.table() read.csv() read.delim()

cars = read.table('https://raw.githubusercontent.com/rsh249/bioinformatics/master/data/mtcars.csv', header=T, sep = ',') # Read a comma separated values file
head(cars)
cars = read.csv('https://raw.githubusercontent.com/rsh249/bioinformatics/master/data/mtcars.csv')

cars = read.delim('https://raw.githubusercontent.com/rsh249/bioinformatics/master/data/mtcars.csv', sep=',')

Write Files

write.table(cars, file='mtcars.tab')
write.csv(cars, file='mtcars.csv')

Where are these files now?

Homework Assignment

  1. Read your data into R.
  2. DISCUSSION: In the slack #discussion channel we will host this week’s discussion. Please search online for an R related resource for R beginners. This could be a book/eBook, a web tutorial, youtube video, or something else. Post what you find. Look through the resource you found and tell the group about it (what it is, how it is structured, who it is targetted to).