Lab 1: Introduction to R#

Please read the document R-intro.pdf. A good tutorial on R is available at www3. In this lab, we will go through the following topics in R.

  • Getting help: google, manual, or ?

  • Numbers, vectors, and matrix, list

  • Math functions

  • String

  • Reading data from files

  • Generating random numbers

  • Loops

  • Writing your own functions

Installing R#

Downloading R at https://cran.microsoft.com/ and following the instruction to install R on your computer. It is highly recommended to install RStudio available at https://posit.co/downloads/ and run R in RStudio. Alternatively, you may run R remotely at https://www.w3schools.com/r/r_intro.asp.

Getting help#

  • Google search “how to calculate average in R”

  • Read the R manual

  • Using ? in R

Assignments#

We may use “=” or “<-” to assign values to variables. For example, we here generate a variable \(x\) whose value is 3.

x <- 3 
x
3

We may generate a vector variable and use “[i]” to locate elements in the vector.

x <- 2:5 
x
x[2]
  1. 2
  2. 3
  3. 4
  4. 5
3

We may generate a string variable which is a vector of characters

x <- "I am a student" 
x
x[3]
'I am a student'
NA

We may generate a matrix variable and use “[i,j]” to locate elements in the vector.

x <- matrix(1:12, nrow=3, ncol=4)
x
x[1,2]
x[1,]
x[,2]
A matrix: 3 × 4 of type int
14710
25811
36912
4
  1. 1
  2. 4
  3. 7
  4. 10
  1. 4
  2. 5
  3. 6

A list variable may contain different types of data

x <- list() 
x[[1]] = "I am a student"
x[[2]] = 123
x
  1. 'I am a student'
  2. 123

Math functions#

x=1:3
x*4 
exp(x) 
log(x) 
x=matrix(1,3,3) 
y=matrix(2,3,3) 
x%*%y 
x*y 
  1. 4
  2. 8
  3. 12
  1. 2.71828182845905
  2. 7.38905609893065
  3. 20.0855369231877
  1. 0
  2. 0.693147180559945
  3. 1.09861228866811
A matrix: 3 × 3 of type dbl
666
666
666
A matrix: 3 × 3 of type dbl
222
222
222

Functions for strings#

x= rep("",3) 
x[1]  = "I am a graduate student"
x[2] = "who am I?" 
x[3] = "who are you?" 

nchar(x[1]) 
substring(x[1],1,3) 
strsplit(x[2], split=" ") 
paste("a","hello",sep="") 
grep("who",x) 
gsub("graduate","undergraduate",x) 
toupper(x) 
tolower(x)
23
'I a'
    1. 'who'
    2. 'am'
    3. 'I?'
'ahello'
  1. 2
  2. 3
  1. 'I am a undergraduate student'
  2. 'who am I?'
  3. 'who are you?'
  1. 'I AM A GRADUATE STUDENT'
  2. 'WHO AM I?'
  3. 'WHO ARE YOU?'
  1. 'i am a graduate student'
  2. 'who am i?'
  3. 'who are you?'

Reading data from files#

data=read.csv("https://book.phylolab.net/binf8441/data/lab1_data.csv") 
data
data[,1] = data[,1] * 10 
write.csv(data,"newdata.csv", row.names=F) 
A data.frame: 25 × 4
V1V2V3V4
<dbl><dbl><dbl><dbl>
0.853327810.747868290.460776030.15329363
0.649143410.726629910.549473330.24407450
0.185422260.263838240.579544350.97183043
0.499886440.939339580.212001220.99106874
0.263741770.986263540.544501770.26758020
0.611127320.332178950.058349720.54131304
0.369142340.080858340.055882110.85876284
0.253507280.837067130.630148170.18264769
0.203570720.258493460.302144840.05598072
0.471164640.673949620.280817470.50700250
0.650123950.010260460.549489440.29967572
0.752236670.077753170.719291150.69480813
0.494427230.491383780.780430930.17591590
0.153645560.166707290.733038670.01809015
0.680467320.444058940.073839230.34538734
0.707168930.028821770.842025710.77440232
0.831295050.086612980.745574300.93672425
0.086382160.787419560.632463970.19764044
0.755388230.095859790.903684840.06294819
0.237113520.358965110.879652620.59115317
0.311841670.752189840.270410380.07943309
0.647906270.055235840.342591540.11879538
0.738036570.613493320.279538610.26276163
0.066453200.707109920.155886450.29456163
0.925410980.229256420.718227350.66264051

Flow control#

x=1 
y=2 

x==y 

x != y 

(x==1) || (y==2) 

(x==0) && (y==2) 
FALSE
TRUE
TRUE
FALSE

Loops#

x = runif(10, min=0, max=1) 
x
sum=0 
for(i in 1:10){
    sum=sum+x[i]
} 
sum 
  1. 0.704896704759449
  2. 0.904335947241634
  3. 0.395604153163731
  4. 0.789092040620744
  5. 0.391858619637787
  6. 0.919142392696813
  7. 0.200044394237921
  8. 0.38271605479531
  9. 0.725150769110769
  10. 0.193563073175028
5.60640414943919
x = 1:10
product = 1 
for(i in 1:10){
    product=product*x[i] 
}
product 
3628800
x = c(5,3,7,2)
n = length(x) 

for(i in 1:(n-1)){ 
    for(j in i:n){ 
        if(x[i]>x[j]){ 
            y=x[i] 
            x[i]=x[j] 
            x[j]=y 
        } 
    } 
} 
x
  1. 2
  2. 3
  3. 5
  4. 7

Constructing your own functions#

my_function_name = function(x, y, …){

….

return(result)

}

my_sum = function(x){ 
    n = length(x)
    sum=0 
    for(i in 1:n){
        sum = sum + x[i] 
    } 
    sum 
}

x = 1:50
my_sum(x)
1275