To start programming in R, we have to understand the below topics first ...
1. Variables/Objects with Data Types: like other programming languages, R also has Numeric (int & float), Caracter (String) and Logical (Boolean) data types. A variable can hold any of this data type data. An object can be any from below.
2. Vectors (one-dimensional array): can hold numeric, character or logical values. The elements in a vector all have the same data type.
3. Factors (Enum): Advanced level of Vactor with category information. Your data can be anything but the category should be limited. Like sex information of a survey, there are two category Male & Female, where the actual Vactor has all data.
4. Matrices (two-dimensional array): can hold numeric, character or logical values. The elements in a matrix all have the same data type.
5. Data frames (two-dimensional Objects): can hold numeric, character or logical values[Variables, Vectors/Factors & Matrices]. Within a column, all elements have the same data type, but different columns can be of a different data type.
6. Lists (Vactor of Objects): This is a Vactor of Objects. Where Objects can be any of Vectors, Factors, Matrices and Data Frames.
Examples:
1. Variable Assignment:
i <- 10
f <- 15.62
c <- "Azam"
l <- TRUE [R is Case Sencitive]
2. Vector (one-dimensional array):
#Define a Vector
idsV <- c(1,2,3,4,5) [Numaric Vector]
idsV <- 1:5 [Numaric Vector]
namesV <- c("Saiful","Azam") [Caracter Vector]
logicalVac <- c(TRUE, FALSE, FALSE, TRUE) [Logical Vector]
# Naming a Vector
namesV <- c("Saiful","Azam")
names(namesV) <- c("FName","LName")
namesV <- c(FName="Saiful",LName="Azam")
#Select elements from a Vector [R is 1-index based not 0-indexBased]
x <- c(9, 5, 6, 3, 7)
x[1,5,3] # Output -- 9 7 6
x[2:4] # Output -- 5 6 3
logicV <- x>5 # logicV = [TRUE, FALSE, TRUE, FALSE, TRUE]
x[logicV] #Output -- 9,6,7
x[!logicV] #Output -- 5,3
#Add/ Delete item to an existing Vector
namesV <- c("Saiful","Azam")
namesV <- c(namesV, "Sohag")
x=1:15
x[! (x>3 & x<10)] # 4-9 will be deleted
x[-(3:10)] # 3-10 will be deleted
x[-(c(3,10))] # 3 & 10 will be deleted
# Calculation with Vector
x + 5 # add 5 to all elements
x * 5 # Multiply 5 with all elements
sum(x) # sum all elements
mean(x) # average of all elements
# Order a Vector using a Col
v1 <- c(10,50,15,66,11,5,90)
ord <- order (v1 , decreasing = FALSE)
v1 [ ord]
v1[order (v1)]
3. Factors (Enum):
#Declare a Factor
sex_vector <- c("Male", "Female", "Female", "Male", "Male")
# Convert sex_vector to a factor
factor_sex_vector <- factor(sex_vector)
#output
[1] Male Female Female Male Male
Levels: Female Male
#Define a Order within Categories in Factor
temperature_vector <- c("High", "Low", "High","Low", "Medium")
factor_temperature_vector <- factor(temperature_vector, order = TRUE, levels = c("Low", "Medium", "High"))
#output
[1] High Low High Low Medium
Levels: Low < Medium < High
#Compare tow elements of a Factor
t1<- temperature_vector [1]
t2 <- temperature_vector [2]
t1<t2 #output -- FALSE
#Define Levels for elements
survey_vector <- c("M", "F", "F", "M", "M")
factor_survey_vector <- factor(survey_vector)
levels(factor_survey_vector) <- c("Female", "Male")
factor_survey_vector
#output
[1] Male Female Female Male Male
Levels: Female Male
summary(factor_survey_vector)
#Output
Female Male
2 3
4. Matrices (two-dimensional array):
#Create Matrix
mx<- matrix(1:9,nrow=3, byrow=TRUE)
v1<- 1:3
v2<- 4:6
v3<- 7:9
vc <- c(v1, v2 ,v3)
mx <- matrix(vc ,nrow=3, byrow=TRUE)
# Naming (Row, Col) a Matrix
rownames(mx) <- c("a","b","c")
colnames(mx) <- c("x","y","z")
mx <- matrix(vc , nrow = 3, byrow = TRUE, dimnames = list(c("a","b","c"), c("x","y","z")))
# Add/ Delete Row & Col in matrix
vAdd <- 10:12
cbind(mx, d = vAdd)
rbind(mx, w = vAdd)
mx[ , -2 ] # Delete 2nd col
mx[ -2, ] # Delete 2nd row
# Select elements from Matrix
mx[ 1, ] # 1st row
mx[ ,1 ] # 1st col
mx[ 1:3, 3 ] # rows 1 to 3 and 3rd col
mx[ 1, 1:3 ] # 1st row and 1-3 cols
mx [ , "a"] # get all row for col "a"
#Get Row/Col Sums/Means
rowSums(mx)
rowMeans(mx)
colSums(mx)
colMeans(mx)
# Calculation with Matrix
mx * 5 # Multiply all elements with 5
mx / 5 # multiply all elements with 5
mx %*% mx # Actual Matrix multiplication
5. Data frames (two-dimensional Objects):
# Define Data-Frame
v1 <- 1:10
v2 <- 11:20
v3 <- 21:30
df <- data.frame(v1, v2, v3)
df <- data.frame(col1=v1, col2=v2, col3=v3) # with diclared column name
# Add/ Delete Col into Data Frame
v4 <- 50:60
df <- data.frame (df , col4 = v4) # add new column col4
df <- df [ , -3 ]
df [ , !(names(df) %in% c("col3")) ]
df [ , !(names(df) == "col3") ]
# Select elements from Data Frames
df[ 1, 2 ]
df [ 1:5, 2:6 ]
df [ , 1:10]
df [ 1:100, ]
df [1:5,"col1"]
df$col3# get all data of col name as vector
df [ df$col3 > 3, ] # all rows where the logical vector is TRUE
subset(df , subset= col2>15) #logicalCondition like x > 2
# Order a Data Frame using a Col
positions <- order (df$col2 , decreasing = FALSE)
df [ positions, ]
6. Lists (Vactor of Objects):
#Define a List
my_vector <- 1:10
my_matrix <- matrix(1:9, ncol = 3)
my_df <- mtcars[1:10,]
my_list <- list(my_vector,my_matrix, my_df)
my_list <- list(vec=my_vector, mat=my_matrix, df=my_df)
# Add/ Delete Object in List
my_list <- c(my_list , year = 2019) #Add
my_list [[2]] <- NULL #DEL
my_list [["vec"]] <- NULL #DEL
#Select elements from a List
my_list [[ 1 ]]
my_list [[ "vec" ]]
my_list [[ "vec" ]][2]
my_list$vec
my_list$vec[2]
No comments:
Post a Comment