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
We may generate a vector variable and use “[i]” to locate elements in the vector.
x <- 2:5
x
x[2]
- 2
- 3
- 4
- 5
We may generate a string variable which is a vector of characters
x <- "I am a student"
x
x[3]
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]
1 | 4 | 7 | 10 |
2 | 5 | 8 | 11 |
3 | 6 | 9 | 12 |
- 1
- 4
- 7
- 10
- 4
- 5
- 6
A list variable may contain different types of data
x <- list()
x[[1]] = "I am a student"
x[[2]] = 123
x
- 'I am a student'
- 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
- 4
- 8
- 12
- 2.71828182845905
- 7.38905609893065
- 20.0855369231877
- 0
- 0.693147180559945
- 1.09861228866811
6 | 6 | 6 |
6 | 6 | 6 |
6 | 6 | 6 |
2 | 2 | 2 |
2 | 2 | 2 |
2 | 2 | 2 |
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)
-
- 'who'
- 'am'
- 'I?'
- 2
- 3
- 'I am a undergraduate student'
- 'who am I?'
- 'who are you?'
- 'I AM A GRADUATE STUDENT'
- 'WHO AM I?'
- 'WHO ARE YOU?'
- 'i am a graduate student'
- 'who am i?'
- '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)
V1 | V2 | V3 | V4 |
---|---|---|---|
<dbl> | <dbl> | <dbl> | <dbl> |
0.85332781 | 0.74786829 | 0.46077603 | 0.15329363 |
0.64914341 | 0.72662991 | 0.54947333 | 0.24407450 |
0.18542226 | 0.26383824 | 0.57954435 | 0.97183043 |
0.49988644 | 0.93933958 | 0.21200122 | 0.99106874 |
0.26374177 | 0.98626354 | 0.54450177 | 0.26758020 |
0.61112732 | 0.33217895 | 0.05834972 | 0.54131304 |
0.36914234 | 0.08085834 | 0.05588211 | 0.85876284 |
0.25350728 | 0.83706713 | 0.63014817 | 0.18264769 |
0.20357072 | 0.25849346 | 0.30214484 | 0.05598072 |
0.47116464 | 0.67394962 | 0.28081747 | 0.50700250 |
0.65012395 | 0.01026046 | 0.54948944 | 0.29967572 |
0.75223667 | 0.07775317 | 0.71929115 | 0.69480813 |
0.49442723 | 0.49138378 | 0.78043093 | 0.17591590 |
0.15364556 | 0.16670729 | 0.73303867 | 0.01809015 |
0.68046732 | 0.44405894 | 0.07383923 | 0.34538734 |
0.70716893 | 0.02882177 | 0.84202571 | 0.77440232 |
0.83129505 | 0.08661298 | 0.74557430 | 0.93672425 |
0.08638216 | 0.78741956 | 0.63246397 | 0.19764044 |
0.75538823 | 0.09585979 | 0.90368484 | 0.06294819 |
0.23711352 | 0.35896511 | 0.87965262 | 0.59115317 |
0.31184167 | 0.75218984 | 0.27041038 | 0.07943309 |
0.64790627 | 0.05523584 | 0.34259154 | 0.11879538 |
0.73803657 | 0.61349332 | 0.27953861 | 0.26276163 |
0.06645320 | 0.70710992 | 0.15588645 | 0.29456163 |
0.92541098 | 0.22925642 | 0.71822735 | 0.66264051 |
Flow control#
x=1
y=2
x==y
x != y
(x==1) || (y==2)
(x==0) && (y==2)
Loops#
x = runif(10, min=0, max=1)
x
sum=0
for(i in 1:10){
sum=sum+x[i]
}
sum
- 0.704896704759449
- 0.904335947241634
- 0.395604153163731
- 0.789092040620744
- 0.391858619637787
- 0.919142392696813
- 0.200044394237921
- 0.38271605479531
- 0.725150769110769
- 0.193563073175028
x = 1:10
product = 1
for(i in 1:10){
product=product*x[i]
}
product
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
- 2
- 3
- 5
- 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)