Lets say we have two sequences
Sequence 1
print(seq1)
## [1] "agagacacagccgaaccccaggcagttttggccggtttgccaggttccccgttaccgtcctt"
& Sequence 2
print(seq2)
## [1] "agagacacagccgaaccccaggcagttttggccgctttgccaggttccccgttaccgtcctt"
And you want to know whether the first sequencing file is the same as the second.
Are the 2 sequences the same?
seq1==seq2
## [1] FALSE
Holds “string” data. For example:
"Hello World"
## [1] "Hello World"
You can ask R to tell you what type of data you have:
class("Hello World")
## [1] "character"
Numbers can be integers or numerics (i.e., floats) in R. For example:
1
## [1] 1
2.2
## [1] 2.2
3E100
## [1] 3e+100
The default for R is to make every number a numeric
class(1)
## [1] "numeric"
Which means that we have hidden decimal points behind the 1 (1.00 for example)
But what if we wanted specifically an integer?
1L
## [1] 1
class(1L)
## [1] "integer"
Is this a numeric or a character?
"2"
## [1] "2"
What is the class(“2”)
class("2")
## [1] "character"
Logicals are either True of False. For example
TRUE
## [1] TRUE
FALSE
## [1] FALSE
class(TRUE)
## [1] "logical"
class(FALSE)
## [1] "logical"
…its time for another challenge question!
R is case sensitive.
With that knowledge, what do you think the below would give us?
What is the class(true)
class(true)
## Error in eval(expr, envir, enclos): object 'true' not found
So far we have only been using class()
But we can also use a function to see the structure of any R object:
str(1)
## num 1
str("Hello World!")
## chr "Hello World!"
str(TRUE)
## logi TRUE
R is an object oriented language, and it tends to put everything in some sort of class. What would the below tell us?
str(class)
What is the str(class)
## function (x)
1+1
## [1] 2
1-1
## [1] 0
2*2
## [1] 4
10/2
## [1] 5
Equivalence comparisons are a way to check if any two objects are the same
#Does 1 equal 1?
1==1
## [1] TRUE
#Does "Hello" equal "World?"
"Hello" == "World"
## [1] FALSE
We can also invert an equivalancy comparison to check if two objects are not equal
#Does 1 not equal 1?
1!=1
## [1] FALSE
For numeric data types, mathematical comparisons can also be made
#Is 1 less than 100?
1<100
## [1] TRUE
#Is 2+2 greater than 2^2
2+2>2^2
## [1] FALSE
Arithmetic in R follows PEMDAS
4+5*3
## [1] 19
vs
(4+5)*3
## [1] 27
What would we get with the following?
4+5*3>(4+5)*3
What would you get with 4+5x3>(4+5)x3
## [1] FALSE
Variables hold objects that are assigned to them.
a <- 1
They can be identical to the object assigned to them
a <- 1
b <- a
#does b equal 1?
b==1
## [1] TRUE
Variables enable complex operations on data
h <- 2^100
i <- h/3E100
j <- 1E5
k <- j^(-1*i)
#is k greater than 1?
k < 1
## [1] FALSE
A Vector is an ordered collection of either numerics, characters, or logicals
c(1, 2, 3)
## [1] 1 2 3
c(TRUE, FALSE, TRUE)
## [1] TRUE FALSE TRUE
c("hello", "world")
## [1] "hello" "world"
A vector in R has a limitation: they can only allow one type of class in each vector.
If you add more than one type of class, it will default to a character.
With that said, what would the following give you?
## [1] "character"
What is the class of (c(“puppies”, 2))
class(c("puppies", 2))
## [1] "character"
I can change the following vector in my variable to all numbers
my_num <- as.numeric(c("2", 2))
class(my_num)
## [1] "numeric"
But what would happen if I tried to do the following?
as.numeric(c("salt", "2"))
print(as.numeric(c("salt", "2")))
What is the class of as.numeric(c(“salt”, “2”))
## Warning in print(as.numeric(c("salt", "2"))): NAs introduced by coercion
## [1] NA 2
## Warning: NAs introduced by coercion
## [1] "numeric"
Vectors can also have a vector of names which describe each element
grades <- c(98, 95, 82)
names(grades) <- c("Jimmy", "Alice", "Susan")
grades
## Jimmy Alice Susan
## 98 95 82
Elements from a vector can be accessed using the index of the desired data. Lets say I wanted to know what Alice’s grade was.
grades[2]
## Alice
## 95
But lets say that I didn’t know Alice was the second index
grades["Alice"]
## Alice
## 95
If you want to add a series of numbers to a vector of integers, you can do the following:
my_ints <- 1:10
my_ints
## [1] 1 2 3 4 5 6 7 8 9 10
A list is an ordered collection of any objects. This differs from vectors, because vectors can only be one type of object class
my_list <- list(1, "b", TRUE, c(1,2,3))
my_list
## [[1]]
## [1] 1
##
## [[2]]
## [1] "b"
##
## [[3]]
## [1] TRUE
##
## [[4]]
## [1] 1 2 3
Lists can also have names
names(my_list)=c("My #", "My Letter", "My logical", "My_num_Set")
my_list
## $`My #`
## [1] 1
##
## $`My Letter`
## [1] "b"
##
## $`My logical`
## [1] TRUE
##
## $My_num_Set
## [1] 1 2 3
The elements within a list can be accessed by using numeric indexes or by the element name
my_list[[2]]
## [1] "b"
my_list[["My_num_Set"]]
## [1] 1 2 3
my_list$My_num_Set
## [1] 1 2 3
#If I did the following, I would get the whole vector within the list
my_list[2]
## $`My Letter`
## [1] "b"
#Data Frames
Data frames are similar to excel sheets. They are 2D arrays which can hold numeric, character, and boolean data. They also have column names.
my_df <- data.frame("students"=c("Jimmy", "Alice", "Susan"),
"Grades"=c(98, 95, 82))
my_df
## students Grades
## 1 Jimmy 98
## 2 Alice 95
## 3 Susan 82
Data frames can be accessed numerically by expressing the row and column of interest.
#What grade did Susan get?
my_df[3,2] #row, column
## [1] 82
They can also be accessed with the $ sign
my_df$Grades
## [1] 98 95 82
#What grade did Alice get?
my_df$Grades[2]
## [1] 95
Thank you!
Questions?
Comments
An important tool for writing R code is comments. These are preceded by # and are ignored by R
Comments are a helpful tool for conveying information about your code to others.