## Practical Exercises on Matrix

• Construct a matrix with 3 rows that contain numbers from 1 to 9
``````basic_matrix <- matrix(1:9, byrow = FALSE, nrow=3)
print(basic_matrix)
``````
• Consider the following collection of movies
``````raise_of_skywalker <- c(515.202, 1072.848)
solo <- c(213.767, 393.151)
last_jedi <- c(620.181, 1055.135)

# create a new vector box_office combining the above there vectors
box_office <- c(raise_of_skywalker, solo, last_jedi)

# Construct a starwars matrix
star_wars_matrix <- matrix(box_office, byrow = TRUE, nrow=3)
print(star_wars_matrix)
``````
• Now add rownames and colnames to the star wars matrix
``````regions <- c("US", "Worldwide")
movies <- c("Raise of Skywalker", "Solo", "Last Jedi")

colnames(star_wars_matrix) <- regions
rownames(star_wars_matrix) <- movies

print(star_wars_matrix)
``````
• Lets try to add the us collections + worldwide collections
``````rowSums(star_wars_matrix)
``````
• Now add this column to the star_wars_matrix
``````all_collections <- cbind(star_wars_matrix, rowSums(star_wars_matrix))
print(all_collections)
`````` ## Factors

• Lets have a gender vector
``````gender_vector <- c("Male", "Female", "Female", "Male", "Female", "Male")
``````
• Lets create a factor gender vector
``````factor_gender_vector <- factor(gender_vector)
print(factor_gender_vector)
``````
• Categorical Variable can be classified as nominal categorical variables and ordinal categories
``````# nominal
fruits_vector <- c("Apple", "Banana", "Mango")
factor_fruits_vector <- factor(fruits_vector)
print(factor_fruits_vector)

# ordinal
bp_vector <- c("High", "Low", "Normal", "High", "Low", "High")

factor_bp_vector <- factor(bp_vector, ordered=TRUE, levels = c("Low", "Normal", "High"))
print(bp_vector)
print(factor_bp_vector)
``````
• Adding levels post factor creation
``````levels(factor_fruits_vector)
levels(factor_bp_vector)

levels(factor_fruits_vector) <- c("Apple", "Banana", "Mango", "Orange")
print(factor_fruits_vector)
``````
• Now lets summarize
``````summary(fruits_vector)
summary(factor_fruits_vector)

summary(bp_vector)
summary(factor_bp_vector)
``````
• In R we have built-in datasets execute `data()` ## DataFrames

• Lets take an existing dataframe `mtcars`
• Now lets try the following
``````head(mtcars)
tail(mtcars)
str(mtcars)
summary(mtcars)
dim(mtcars)
nrow(mtcars)
ncol(mtcars)
``````
• Sort mtcars dataframe by mpg (miles per gallon) in decreasing
``````positions <- order(mtcars\$mpg, decreasing = TRUE)
print(positions)

mtcars[positions, ]
``````
• sort rock by area

• Install tidyverse package `install.packages('tidyverse')` Refer Here

This site uses Akismet to reduce spam. Learn how your comment data is processed. 