Type the following basic commands and check their outputs
5+5
## [1] 10
100+20+40
## [1] 160
58-10
## [1] 48
100-20-10
## [1] 70
The asterisk (*) is used when performing multiplication in R
5*4
## [1] 20
200*5*20
## [1] 20000
10/5
## [1] 2
100/2.5
## [1] 40
What will be the result of this equation?
2+5*(100/10)
## [1] 52
2^3
## [1] 8
10^4
## [1] 10000
What do you think the Modulus operator is used for?
50 %% 7
## [1] 1
100 %% 10
## [1] 0
25 %% 2
## [1] 1
So far you have computed few values but they are not saved anywhere to use them again. In order to save the results we need to declare VARIABLES
Variables are the objects used to store/save the values. For example the following command will declare the variable with a name a that will store the value 10.
a <- 10
Another example
x <- 100
Note: R is a case sensitive language. The a and A will be treated as two different variables.
A <- 50
In order to update the value of any variable just write the variable name and assign a new value to it. For example this command will update the vlaue of a to 5.
a <- 5
You can also perform mathematical opertaions on the variable names
A + a
## [1] 55
A / a
## [1] 10
C <- a + 10 + (A * 2)
C
## [1] 115
Variables have a certain naming conventions. What will happen if you will name your variable as 5a. For eg 5a <- 10
rm(a)
So far we have dealed with Numbers. The data classes in R can be broadly classified into 3 major types.
The variables and the values that we had used before they all belonged to the broad numeric category. In the similar way we can assign text to variables. Tip: make sure the text is enclosed in double quotes to avoid any error. For eg
my_name <- "Siddhant"
This will create a variable named my_name and will have the value Siddhant. Now create a variable that will store your name.
course_name <- "BIOL310 Genomics Analysis"
course_name
## [1] "BIOL310 Genomics Analysis"
Logical class (also know as the Boolean class) contains the value TRUE or FALSE
value1 <- TRUE
value2 <- FALSE
Know the class of a variable
class(value1)
## [1] "logical"
class(course_name)
## [1] "character"
a <- 20
class(a)
## [1] "numeric"
R also provides an option to convert classes into one another.
For example the numeric class can be converted into character class.
b <- as.character(a)
Print the value and class of both the variables a and b
a
## [1] 20
class(a)
## [1] "numeric"
b
## [1] "20"
class(b)
## [1] "character"
Since the value in b (in this case 20) is now treated as a character.
You cannot perform mathematical opertaions on it
anymore.
Try to add 5 to b
Most of the time we will be working with multiple data types (numeric, character, logical) at a same time. R has a different data structures for handling these things.
In the next class we will start our discussion focusing on these four main data structures.