dplyr
ggplot2
rstudio
Basically, there are 5 naming conventions
mynamenew.namemy_namemyNameMyNameUsing equal(=) operator
x = 10
Using leftward(<-) operator
y <- 15
?Reserved
At the R prompt/console we type expressions.
num <- 10
The <- symbol is the assignment operator.
The grammar of the language determines whether an expression is complete or not.
When a complete expression is entered at the R console, it is evaluated and the result of evaluated expression is returned. The result may be auto-printed.
x <- 10
x
[1] 10
x <- 10
print(x)
[1] 10
x <- 10
cat(x)
10
R has five basic data types
L suffix.Inf which is represents infinity; e.g. 1 / 0Inf can be used in ordinary calculations; e.g. 1/Inf is 0NaN represents an undefined value(“not a number”); e.g. 0/0NaN can also be thought of as a missing value.The numeric constants are
L)i)# Create numeric object
n <- 5
# Check type of object
typeof(n)
[1] "double"
# Create an integer type object
i = 5L
# Check type of object
typeof(i)
[1] "integer"
# Create a double type object
d = 22
# Check type of object
typeof(d)
[1] "double"
# Create a complex type object
c = 4i
# Check type of object
typeof(c)
[1] "complex"
The Decimal, Double, and Float variable types are different in the way that they store the values. Precision is the main difference where float is a single precision (32 bit) floating point data type, double is a double precision (64 bit) floating point data type and decimal is a 128-bit floating point data type.
Character constants can be represented using either single quotes (’’) or double quotes ("") as delimiters.
# Create a character type object
char = "Hello"
# Check type of object
typeof(char)
[1] "character"
R objects can have attributes
Attributes of an object can be accessed using the attributes() function.
pi # value of pi
[1] 3.141593
LETTERS
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
[20] "T" "U" "V" "W" "X" "Y" "Z"
month.name
[1] "January" "February" "March" "April" "May" "June"
[7] "July" "August" "September" "October" "November" "December"
x <- 10
y <- 2
# Addition
x+y
[1] 12
# Subtraction
2-5
[1] -3
# Multiplication
2 * 5
[1] 10
# Division
2 / 5
[1] 0.4
# Exponent
2 ^ 5
[1] 32
# Modulus(Remainder from division)
2 %% 5
[1] 2
# Integer Division
2 %/% 5
[1] 0
# Logical NOT(!)
! TRUE
[1] FALSE
! FALSE
[1] TRUE
# Logical AND(&&)
TRUE && TRUE
[1] TRUE
TRUE && FALSE
[1] FALSE
FALSE && FALSE
[1] FALSE
# Logical OR(|)
TRUE | TRUE
[1] TRUE
TRUE | FALSE
[1] TRUE
FALSE | FALSE
[1] FALSE
x <- 10
y <- 5
# Equality
x == y
[1] FALSE
# Inequality
x != y
[1] TRUE
# Less Than
x < y
[1] FALSE
# Greater Than
x > y
[1] TRUE
# Less or Equal
x <= y
[1] FALSE
# Greater or Equal
x >= y
[1] TRUE
In computer science, a data structure is a data organization, management, and storage format that enables efficient access and modification. More precisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data. (https://en.wikipedia.org/wiki/Data_structure)
typeof() function.# Create a vector using c() function
v1 <- c(1, 2, 3, 4, 5, 6,7)
# print v1
v1
[1] 1 2 3 4 5 6 7
# Create a vector using : operator
v2 <- 1:10
# print v2
v2
[1] 1 2 3 4 5 6 7 8 9 10
# Create a vector using seq(start, stop, step) function
v3 <- seq(1, 20, 2)
# print v3
v3
[1] 1 3 5 7 9 11 13 15 17 19
# Create a vector using seq(start, stop, by = step) function
v4 <- seq(1, 10, by = .5)
# print v3
v4
[1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0
[16] 8.5 9.0 9.5 10.0
# Numeric Vector
v <- c(0.5, 0.6)
# Logical Vector
v <- c(TRUE, FALSE)
# Logical Vector
v <- c(T, F)
# Character Vector
v <- c("a", "b")
# Integer Vector
v <- 1:10
# Complex Vector
v <- c(1+0i, 2+0i)
# Character
x <- c(1.7, "a")
# Numeric
y <- c(TRUE, 2)
# Character
z <- c("a", TRUE)
attributes() function.dim() function.class() function.matrix() function.nrow and ncol.# Create a matrix using matrix function
mat <- matrix(1:9, nrow = 3, ncol = 3)
# print matrix
mat
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
# Create a matrix using matrix function: only one dimension
mat <- matrix(1:9, nrow = 3)
# print matrix
mat
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
# Create a matrix using matrix function: filling by row-wise
mat <- matrix(1:9, nrow = 3, byrow = TRUE)
# print matrix
mat
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
# Create a matrix using matrix function: dimension names
mat <- matrix(1:9, nrow = 3, dimnames = list(c("X", "Y", "Z"),
c("A","B","C")))
# print matrix
mat
A B C
X 1 4 7
Y 2 5 8
Z 3 6 9
# Create a matrix using matrix function
mat <- matrix(1:9, nrow = 3, dimnames = list(c("X", "Y", "Z"),
c("A","B","C")))
# Column Names
colnames(mat)
[1] "A" "B" "C"
# Row Names
rownames(mat)
[1] "X" "Y" "Z"
# Dimension
dim(mat)
[1] 3 3
vector having all elements of the same type is called atomic vector but a vector having elements of different type is called list.typeof() function and find its length using length() function.List can be created using the list() function.
# Create a list
L = list(1, "a", TRUE, 1+3i)
# Print list
L
[[1]]
[1] 1
[[2]]
[1] "a"
[[3]]
[1] TRUE
[[4]]
[1] 1+3i
# Create a factor using factor() function
f <- factor(c("yes", "no", "yes", "no"))
# Print factor
f
[1] yes no yes no
Levels: no yes
# Check levels
levels(f)
[1] "no" "yes"
list which has each component of equal length.# Create a Data Frame
df <- data.frame("Age" = c(21, 22, 14, 15, 16, 23),
"Name" = c("Jim","Tim", "Babul", "Bithi", "Abul", "Akhi"),
"Married" = factor(c("yes", "no", "yes", "no", "yes", "yes")))
# Print Data Frame
df
Age Name Married
1 21 Jim yes
2 22 Tim no
3 14 Babul yes
4 15 Bithi no
5 16 Abul yes
6 23 Akhi yes
# Dimension
dim(df)
[1] 6 3
# Data Structures
str(df)
'data.frame': 6 obs. of 3 variables:
$ Age : num 21 22 14 15 16 23
$ Name : chr "Jim" "Tim" "Babul" "Bithi" ...
$ Married: Factor w/ 2 levels "no","yes": 2 1 2 1 2 2
# Summary
summary(df)
Age Name Married
Min. :14.00 Length:6 no :2
1st Qu.:15.25 Class :character yes:4
Median :18.50 Mode :character
Mean :18.50
3rd Qu.:21.75
Max. :23.00
# Colnames
names(df)
[1] "Age" "Name" "Married"
# Accessing Columns
df$Name
[1] "Jim" "Tim" "Babul" "Bithi" "Abul" "Akhi"
# Create a vector
x <- c(1, 11, 111)
# Extract first element
x[1]
[1] 1
# Extract second element
x[2]
[1] 11
# Create a vector
x <- c("a", "b", "c", "d", "e")
# Extract data using :
x[1:3]
[1] "a" "b" "c"
# Extract data using c() function
x[c(1, 3, 4)]
[1] "a" "c" "d"
# Create a vector
x <- c(1, 20, 21, 11, 23, 40, 42)
# Extract data using relational operator
x[x > 32]
[1] 40 42
L = list(x = 1:5, y=0.5)
# Extract first element
L[[1]]
[1] 1 2 3 4 5
# Extract first element
L[[2]]
[1] 0.5
# Access elements
L$x
[1] 1 2 3 4 5
m <- matrix(1:6, 2, 3)
m
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
# Extract first row
m[1, ]
[1] 1 3 5
# Extract first element from first row
m[1, 1]
[1] 1
m <- matrix(1:6, 2, 3)
m
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
# Extract second column
m[, 2]
[1] 3 4
if statementif(condition) {
# do something
}
num = 10
if (num > 0) {
print("positive number")
}
[1] "positive number"
if..else statementif(condition) {
# do something
} else {
# do something
}
num = 10
if (num > 0) {
print("positive number")
} else {
print("Negative number")
}
[1] "positive number"
if..else if...else statementif(condition-1) {
# do something
} else if(condition-2){
# do something
} else if(condition-3) {
# do something
} else {
# do something
}
bmi = 18.5
if(bmi < 18.5) {
print("underweight")
} else if(bmi >= 18.5 && bmi < 25){
print("normal")
} else if(bmi <= 25 && bmi < 30 ){
print("overweight")
} else {
print("obese")
}
[1] "normal"
ifelse functionif...else statement in R, the ifelse() function.ifelse(condition, x, y)
a = 10
ifelse(a %% 2 == 0, "even", "odd")
[1] "even"
b = c(22, 12, 23, 24, 21, 28)
ifelse(b %% 2 == 0, "even", "odd")
[1] "even" "even" "odd" "even" "odd" "even"
for loopfor (val in sequence){
# do something
}
for (i in 1:10) {
print(i)
}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
for (i in 10:1) {
print(i)
}
[1] 10
[1] 9
[1] 8
[1] 7
[1] 6
[1] 5
[1] 4
[1] 3
[1] 2
[1] 1
L = c(2, 1, 4, 5, 6, 7)
for (val in L) {
print(val)
}
[1] 2
[1] 1
[1] 4
[1] 5
[1] 6
[1] 7
L = c(2, 1, 4, 5, 6, 7)
for (val in L) {
if(val %% 2 == 0) {
print(val)
}
}
[1] 2
[1] 4
[1] 6
while loopwhile(condition){
# do something
}
i = 1
while (i <= 10) {
print(i)
i = i + 1
}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
i = 10
while(i > 0) {
print(i)
i = i -1
}
[1] 10
[1] 9
[1] 8
[1] 7
[1] 6
[1] 5
[1] 4
[1] 3
[1] 2
[1] 1
x <- 1:10
for (i in x) {
if(i == 5) {
break
}
print(i)
}
[1] 1
[1] 2
[1] 3
[1] 4
i = 0
while (i <= 10) {
i = i + 1
if(i == 5) {
break
}
print(i)
}
[1] 1
[1] 2
[1] 3
[1] 4
for (val in 1:10) {
if (val == 5) {
next
}
print(val)
}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
i = 0
while(i <= 5) {
i = i + 1
if(i == 2) {
next
}
print(i)
}
[1] 1
[1] 3
[1] 4
[1] 5
[1] 6
x <- 1
repeat {
print(x)
x = x+1
if (x == 6){
break
}
}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
seq(), mean(), max(), sum() and paste(…) etc. They are directly called by user written programs.f <- function() {
# empty function
}
# Function have their own class
class(f)
[1] "function"
# Execute / Call this function
f()
NULL
# Create a function with no arguments
say_hello <- function() {
cat("Hello World\n")
}
# Call
say_hello()
Hello World
say_hello <- function(num) {
for(i in seq_len(num)) {
cat("Hello World!\n")
}
}
# Call function with arguments
say_hello(3)
Hello World!
Hello World!
Hello World!
Comments in R
The # character indicates a comment.
Anything to the right of the # (inlcuding the # itself) is ignored.
This is only comment character in R.
R does not support multi-line comments or comment block.