Basic operations in R language
# Define variables with different data types
numeric_var <- 42
character_var <- "welcome to the spoyify!"
logical_var <- TRUE
date_time_var <- as.POSIXct("2023-01-15 14:30:00")
# Print variables
cat("Numeric Variable:", numeric_var, "\n")
## Numeric Variable: 42
cat("Character Variable:", character_var, "\n")
## Character Variable: welcome to the spoyify!
# Create data structures
vector_example <- c(1, 2, 3, 4, 5)
matrix_example <- matrix(1:6, nrow = 2, ncol = 3)
list_example <- list(1, "apple", TRUE)
data_frame_example <- data.frame(
Name = c("taylor swift", "the weeknd xoxo", "Charlie Puth"),
Age = c(25, 30, 22),
Artist_count = c(90, 85, 92)
)
# Print data structures
cat("Vector Example:", vector_example, "\n")
## Vector Example: 1 2 3 4 5
cat("Matrix Example:\n")
## Matrix Example:
print(matrix_example)
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
cat("List Example:\n")
## List Example:
print(list_example)
## [[1]]
## [1] 1
##
## [[2]]
## [1] "apple"
##
## [[3]]
## [1] TRUE
cat("Data Frame Example:\n")
## Data Frame Example:
print(data_frame_example)
## Name Age Artist_count
## 1 taylor swift 25 90
## 2 the weeknd xoxo 30 85
## 3 Charlie Puth 22 92
# a.Create two vectors of integers
vector1 <- c(1, 2, 3)
vector2 <- c(4, 5, 6)
# Add the two vectors element-wise
result_vector <- vector1 + vector2
# Print the result
print(result_vector)
## [1] 5 7 9
# b.Create a vector
my_vector <- c(2, 4, 6, 8, 10)
# Calculate the sum, mean, and product
sum_result <- sum(my_vector)
mean_result <- mean(my_vector)
product_result <- prod(my_vector)
# Print the results
print(paste("Sum:", sum_result))
## [1] "Sum: 30"
print(paste("Mean:", mean_result))
## [1] "Mean: 6"
print(paste("Product:", product_result))
## [1] "Product: 3840"
# c.Create a vector
my_vector <- c(3, 1, 7, 2, 9)
# Find the minimum and maximum
min_value <- min(my_vector)
max_value <- max(my_vector)
# Print the results
print(paste("Minimum:", min_value))
## [1] "Minimum: 1"
print(paste("Maximum:", max_value))
## [1] "Maximum: 9"
#d. Create a list
my_list <- list(
string_element = "Hello, World",
numeric_element = 42,
vector_element = c(1, 2, 3),
logical_element = TRUE
)
# Print the list
print(my_list)
## $string_element
## [1] "Hello, World"
##
## $numeric_element
## [1] 42
##
## $vector_element
## [1] 1 2 3
##
## $logical_element
## [1] TRUE
#e. Create a list with named elements
my_list <- list(
vector_element = c(1, 2, 3),
matrix_element = matrix(1:6, nrow = 2),
nested_list = list(a = "apple", b = "banana")
)
# Access the first and second elements of the list
first_element <- my_list$vector_element
second_element <- my_list$matrix_element
# Print the accessed elements
print(first_element)
## [1] 1 2 3
print(second_element)
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
#f. Create a 3x5 matrix filled with zeros
my_matrix <- matrix(0, nrow = 3, ncol = 5)
# Print the matrix
print(my_matrix)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0 0 0 0 0
## [2,] 0 0 0 0 0
## [3,] 0 0 0 0 0
#g. Create a sample matrix5 7 6
5
## [1] 5
my_matrix <- matrix(1:12, nrow = 3)
# Access specific elements
element_1 <- my_matrix[2, 3] # 3rd column, 2nd row
element_2 <- my_matrix[3, ] # 3rd row
element_3 <- my_matrix[, 4] # 4th column
# Print the accessed elements
print(element_1)
## [1] 8
print(element_2)
## [1] 3 6 9 12
print(element_3)
## [1] 10 11 12
#h. Create vectors
name <- c("Alice", "Bob", "Charlie")
age <- c(25, 30, 35)
# Create a DataFrame
df <- data.frame(Name = name, Age = age)
# Display the DataFrame
print(df)
## Name Age
## 1 Alice 25
## 2 Bob 30
## 3 Charlie 35
#i. Create a DataFrame
df <- data.frame(Name = c("Alice", "Bob"), Age = c(25, 30))
# New data to insert
new_data <- data.frame(Name = c("Charlie", "David"), Age = c(35, 40))
# Insert new rows
df <- rbind(df, new_data)
# Display the updated DataFrame
print(df)
## Name Age
## 1 Alice 25
## 2 Bob 30
## 3 Charlie 35
## 4 David 40
#j. Create a DataFrame
df <- data.frame(Name = c("Alice", "Bob"), Age = c(25, 30))
# Add a new column
df$Salary <- c(50000, 60000)
# Display the updated DataFrame
print(df)
## Name Age Salary
## 1 Alice 25 50000
## 2 Bob 30 60000
#k. Create a DataFrame
df <- data.frame(Name = c("Alice", "Bob", "Charlie", "David"), Age = c(25, 30, 35, 40))
# Extract the first 2 rows
first_two_rows <- df[1:2, ]
# Display the extracted rows
print(first_two_rows)
## Name Age
## 1 Alice 25
## 2 Bob 30
#l. Create a DataFrame
df <- data.frame(Name = c("charlie puth", "Alice murphy", "Bob marley"), Age = c(35, 25, 30))
# Sort the DataFrame by the "Age" column
sorted_df <- df[order(df$Age), ]
# Display the sorted DataFrame
print(sorted_df)
## Name Age
## 2 Alice murphy 25
## 3 Bob marley 30
## 1 charlie puth 35
#m. Create two DataFrames
df1 <- data.frame(ID = 1:3, Name = c("Alice murphy", "Bob marley", "Charlie puth"))
df2 <- data.frame(ID = 2:4, Salary = c(50000, 60000, 70000))
# Merge the DataFrames based on the "ID" column
merged_df <- merge(df1, df2, by = "ID", all = TRUE)
# Display the merged DataFrame
print(merged_df)
## ID Name Salary
## 1 1 Alice murphy NA
## 2 2 Bob marley 50000
## 3 3 Charlie puth 60000
## 4 4 <NA> 70000
#n. Create two DataFrames
df1 <- data.frame(Name = c("Alice", "Bob"), Age = c(25, 30))
df2 <- data.frame(Name = c("Charlie", "David"), Age = c(35, 40))
# Append df2 to the end of df1
appended_df <- rbind(df1, df2)
# Display the appended DataFrame
print(appended_df)
## Name Age
## 1 Alice 25
## 2 Bob 30
## 3 Charlie 35
## 4 David 40
#p. Create two dataframes
df1 <- data.frame(ID = 1:4, Name = c("Alice", "Bob", "Charlie", "David"))
df2 <- data.frame(ID = 2:5, Salary = c(50000, 60000, 70000, 55000))
# Merge the dataframes based on the "ID" column
merged_df <- merge(df1, df2, by = "ID", all = TRUE)
# Display the merged dataframe
print(merged_df)
## ID Name Salary
## 1 1 Alice NA
## 2 2 Bob 50000
## 3 3 Charlie 60000
## 4 4 David 70000
## 5 5 <NA> 55000
#q.a. Read data from the console
data <- as.numeric(readline("Enter a number: "))
## Enter a number:
` importing a csv file
data=read.csv("C://Users//java//Downloads//student.csv")
head(data)
## STUDENT.ID Student.Age Sex Graduated.high.school.type Scholarship.type
## 1 STUDENT1 2 2 3 3
## 2 STUDENT2 2 2 3 3
## 3 STUDENT3 2 2 2 3
## 4 STUDENT4 1 1 1 3
## 5 STUDENT5 2 2 1 3
## 6 STUDENT6 2 2 2 3
## Additional.work Regular.artistic.or.sports.activity Do.you.have.a.partner
## 1 1 2 2
## 2 1 2 2
## 3 2 2 2
## 4 1 2 1
## 5 2 2 1
## 6 2 2 2
## Total.salary.if.available Transportation.to.the.university
## 1 1 1
## 2 1 1
## 3 2 4
## 4 2 1
## 5 3 1
## 6 2 1
## Accommodation.type.in.Cyprus Mother.s.education Father.s.education
## 1 1 1 2
## 2 1 2 3
## 3 2 2 2
## 4 2 1 2
## 5 4 3 3
## 6 1 3 3
## Number.of.sisters.brothers Parental.status Mother.s.occupation
## 1 3 1 2
## 2 2 1 2
## 3 2 1 2
## 4 5 1 2
## 5 2 1 2
## 6 2 1 2
## Father.s.occupation Weekly.study.hours Reading.frequency Reading.frequency.1
## 1 5 3 2 2
## 2 1 2 2 2
## 3 1 2 1 2
## 4 1 3 1 2
## 5 4 2 1 1
## 6 3 1 1 2
## Attendance.to.the.seminars.conferences.related.to.the.department
## 1 1
## 2 1
## 3 1
## 4 1
## 5 1
## 6 1
## Impact.of.your.projects.activities.on.your.success Attendance.to.classes
## 1 1 1
## 2 1 1
## 3 1 1
## 4 1 1
## 5 1 1
## 6 1 1
## Preparation.to.midterm.exams.1 Preparation.to.midterm.exams.2
## 1 1 1
## 2 1 1
## 3 1 1
## 4 1 2
## 5 2 1
## 6 1 1
## Taking.notes.in.classes Listening.in.classes
## 1 3 2
## 2 3 2
## 3 2 2
## 4 3 2
## 5 2 2
## 6 1 2
## Discussion.improves.my.interest.and.success.in.the.course Flip.classroom
## 1 1 2
## 2 3 2
## 3 1 1
## 4 2 1
## 5 2 1
## 6 1 2
## Cumulative.grade.point.average.in.the.last.semester...4.00.
## 1 1
## 2 2
## 3 2
## 4 3
## 5 2
## 6 4
## Expected.Cumulative.grade.point.average.in.the.graduation...4.00. COURSE.ID
## 1 1 1
## 2 3 1
## 3 2 1
## 4 2 1
## 5 2 1
## 6 4 1
## GRADE
## 1 1
## 2 1
## 3 1
## 4 1
## 5 1
## 6 2
tail(data)
## STUDENT.ID Student.Age Sex Graduated.high.school.type Scholarship.type
## 140 STUDENT140 1 1 2 4
## 141 STUDENT141 2 1 2 3
## 142 STUDENT142 1 1 2 4
## 143 STUDENT143 1 1 1 4
## 144 STUDENT144 2 1 2 4
## 145 STUDENT145 1 1 1 5
## Additional.work Regular.artistic.or.sports.activity Do.you.have.a.partner
## 140 1 2 1
## 141 1 1 2
## 142 2 2 2
## 143 2 2 2
## 144 1 1 1
## 145 2 2 2
## Total.salary.if.available Transportation.to.the.university
## 140 2 2
## 141 1 1
## 142 1 4
## 143 1 1
## 144 5 2
## 145 3 1
## Accommodation.type.in.Cyprus Mother.s.education Father.s.education
## 140 3 3 3
## 141 2 1 2
## 142 2 1 1
## 143 1 3 4
## 144 3 4 4
## 145 1 3 1
## Number.of.sisters.brothers Parental.status Mother.s.occupation
## 140 1 1 5
## 141 2 2 2
## 142 5 1 2
## 143 4 1 2
## 144 1 1 3
## 145 5 1 2
## Father.s.occupation Weekly.study.hours Reading.frequency
## 140 4 1 1
## 141 4 3 3
## 142 1 3 2
## 143 4 2 2
## 144 3 2 2
## 145 4 3 1
## Reading.frequency.1
## 140 2
## 141 2
## 142 2
## 143 2
## 144 1
## 145 1
## Attendance.to.the.seminars.conferences.related.to.the.department
## 140 2
## 141 1
## 142 2
## 143 1
## 144 1
## 145 1
## Impact.of.your.projects.activities.on.your.success Attendance.to.classes
## 140 1 2
## 141 1 1
## 142 1 2
## 143 1 1
## 144 1 1
## 145 1 1
## Preparation.to.midterm.exams.1 Preparation.to.midterm.exams.2
## 140 2 1
## 141 1 1
## 142 1 1
## 143 1 1
## 144 2 1
## 145 2 1
## Taking.notes.in.classes Listening.in.classes
## 140 2 3
## 141 2 1
## 142 3 2
## 143 3 3
## 144 2 1
## 145 3 2
## Discussion.improves.my.interest.and.success.in.the.course Flip.classroom
## 140 2 1
## 141 2 1
## 142 2 1
## 143 2 1
## 144 2 1
## 145 3 1
## Cumulative.grade.point.average.in.the.last.semester...4.00.
## 140 1
## 141 3
## 142 5
## 143 4
## 144 5
## 145 5
## Expected.Cumulative.grade.point.average.in.the.graduation...4.00. COURSE.ID
## 140 2 9
## 141 3 9
## 142 3 9
## 143 3 9
## 144 3 9
## 145 4 9
## GRADE
## 140 0
## 141 5
## 142 5
## 143 1
## 144 4
## 145 3