INITIAL COMMIT
This commit is contained in:
commit
25f0531dcd
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
.Rproj.user
|
||||||
|
.Rhistory
|
||||||
|
.RData
|
||||||
|
.Ruserdata
|
BIN
Rplots.pdf
Normal file
BIN
Rplots.pdf
Normal file
Binary file not shown.
BIN
Rplots1.pdf
Normal file
BIN
Rplots1.pdf
Normal file
Binary file not shown.
53
apply.R
Normal file
53
apply.R
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
# Creating a matrix for demonstration
|
||||||
|
matrix_data <- matrix(1:9, nrow=3, byrow=TRUE)
|
||||||
|
print("Original Matrix:")
|
||||||
|
print(matrix_data)
|
||||||
|
|
||||||
|
# Using apply on a matrix
|
||||||
|
# -----------------------
|
||||||
|
# Sum of each row
|
||||||
|
row_sums <- apply(matrix_data, 1, sum)
|
||||||
|
print("Sum of each row:")
|
||||||
|
print(row_sums)
|
||||||
|
|
||||||
|
# Sum of each column
|
||||||
|
col_sums <- apply(matrix_data, 2, sum)
|
||||||
|
print("Sum of each column:")
|
||||||
|
print(col_sums)
|
||||||
|
|
||||||
|
# Creating a list for demonstration
|
||||||
|
list_data <- list(a = 1:5, b = 6:10, c = 11:15)
|
||||||
|
print("Original List:")
|
||||||
|
print(list_data)
|
||||||
|
|
||||||
|
# Using lapply on a list
|
||||||
|
# ----------------------
|
||||||
|
# Compute the mean of each element in the list
|
||||||
|
list_means <- lapply(list_data, mean)
|
||||||
|
print("Mean of each list element:")
|
||||||
|
print(list_means)
|
||||||
|
|
||||||
|
# Using sapply on a list
|
||||||
|
# ----------------------
|
||||||
|
# sapply simplifies the result to a vector or matrix if possible
|
||||||
|
list_means_simplified <- sapply(list_data, mean)
|
||||||
|
print("Simplified mean of each list element:")
|
||||||
|
print(list_means_simplified)
|
||||||
|
|
||||||
|
# Using vapply on a list
|
||||||
|
# ----------------------
|
||||||
|
# vapply allows for specifying the type of return value, making it safer than sapply
|
||||||
|
list_means_vapply <- vapply(list_data, mean, numeric(1))
|
||||||
|
print("Vapply mean of each list element:")
|
||||||
|
print(list_means_vapply)
|
||||||
|
|
||||||
|
# Creating a vector and factor for demonstration
|
||||||
|
vector_data <- c(1, 2, 3, 4, 5, 6)
|
||||||
|
factor_data <- gl(2, 3, labels = c("Group1", "Group2"))
|
||||||
|
|
||||||
|
# Using tapply on a vector
|
||||||
|
# ------------------------
|
||||||
|
# Apply a function over subsets of a vector
|
||||||
|
group_means <- tapply(vector_data, factor_data, mean)
|
||||||
|
print("Mean of vector elements by group:")
|
||||||
|
print(group_means)
|
42
classes.R
Normal file
42
classes.R
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
# S3 and S4 are two different object-oriented programming (OOP) systems in R.
|
||||||
|
# S3 is a simple and informal OOP system, while S4 is a more formal and rigorous OOP system.
|
||||||
|
|
||||||
|
|
||||||
|
# S3 Classes
|
||||||
|
# ----------
|
||||||
|
# Creating an S3 class
|
||||||
|
create_person_s3 <- function(name, age) {
|
||||||
|
obj <- list(name=name, age=age)
|
||||||
|
class(obj) <- "PersonS3"
|
||||||
|
return(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
# Creating a print method for the S3 class
|
||||||
|
print.PersonS3 <- function(x) {
|
||||||
|
cat("PersonS3: Name =", x$name, "- Age =", x$age, "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
# Example of creating and printing an S3 object
|
||||||
|
person_s3 <- create_person_s3("Alice", 30)
|
||||||
|
print(person_s3)
|
||||||
|
|
||||||
|
# S4 Classes
|
||||||
|
# ----------
|
||||||
|
# Defining an S4 class
|
||||||
|
setClass("PersonS4",
|
||||||
|
slots = list(name = "character", age = "numeric"))
|
||||||
|
|
||||||
|
# Creating a constructor for the S4 class
|
||||||
|
create_person_s4 <- function(name, age) {
|
||||||
|
obj <- new("PersonS4", name=name, age=age)
|
||||||
|
return(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
# Creating a show method for the S4 class (similar to print method)
|
||||||
|
setMethod("show", "PersonS4", function(object) {
|
||||||
|
cat("PersonS4: Name =", object@name, "- Age =", object@age, "\n")
|
||||||
|
})
|
||||||
|
|
||||||
|
# Example of creating and showing an S4 object
|
||||||
|
person_s4 <- create_person_s4("Bob", 25)
|
||||||
|
show(person_s4)
|
58
create_lists.R
Normal file
58
create_lists.R
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
# 1. Creating a simple list with mixed types
|
||||||
|
simple_list <- list(name="John", age=25, scores=c(88, 92, 85))
|
||||||
|
print("Simple List:")
|
||||||
|
print(simple_list)
|
||||||
|
|
||||||
|
# 2. Creating a list using the list() function with named elements
|
||||||
|
named_list <- list(firstName="Alice", lastName="Smith", details=list(age=30, profession="Data Scientist"))
|
||||||
|
print("Named List with Nested List:")
|
||||||
|
print(named_list)
|
||||||
|
|
||||||
|
# 3. Creating a list from vectors
|
||||||
|
vector1 <- 1:5
|
||||||
|
vector2 <- seq(10, 50, by=10)
|
||||||
|
vector_list <- list(vector1, vector2)
|
||||||
|
print("List from Vectors:")
|
||||||
|
print(vector_list)
|
||||||
|
|
||||||
|
# 4. Modifying a list by adding an element
|
||||||
|
simple_list$gender <- "male" # Adding a new element
|
||||||
|
print("Modified List (Added Element):")
|
||||||
|
print(simple_list)
|
||||||
|
|
||||||
|
# 5. Modifying a list by removing an element
|
||||||
|
simple_list$scores <- NULL # Removing an element
|
||||||
|
print("Modified List (Removed Element):")
|
||||||
|
print(simple_list)
|
||||||
|
|
||||||
|
# 6. Accessing elements in a list
|
||||||
|
# Access by element name
|
||||||
|
print("Accessed Element (Name):")
|
||||||
|
print(named_list$firstName)
|
||||||
|
# Access by index
|
||||||
|
print("Accessed Element (Index):")
|
||||||
|
print(named_list[[2]])
|
||||||
|
|
||||||
|
# 7. Concatenating lists
|
||||||
|
concatenated_list <- c(simple_list, named_list)
|
||||||
|
print("Concatenated List:")
|
||||||
|
print(concatenated_list)
|
||||||
|
|
||||||
|
# 8. Using lapply to apply a function to each element of a list
|
||||||
|
# Here we use the length function to find the length of each element
|
||||||
|
lengths <- lapply(vector_list, length)
|
||||||
|
print("Lengths of List Elements:")
|
||||||
|
print(lengths)
|
||||||
|
|
||||||
|
# 9. Creating a list with repeated elements using rep()
|
||||||
|
repeated_list <- list(rep(list(x = 1:3), 3))
|
||||||
|
print("List with Repeated Elements:")
|
||||||
|
print(repeated_list)
|
||||||
|
|
||||||
|
# 10. Nested lists
|
||||||
|
nested_list <- list(
|
||||||
|
sub_list1 = list(item1 = 1, item2 = 2),
|
||||||
|
sub_list2 = list(item1 = "a", item2 = "b")
|
||||||
|
)
|
||||||
|
print("Nested List:")
|
||||||
|
print(nested_list)
|
60
create_matrices.R
Normal file
60
create_matrices.R
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
# 1. Creating a matrix from a vector using the matrix() function
|
||||||
|
matrix1 <- matrix(1:9, nrow=3, ncol=3)
|
||||||
|
print("Matrix from Vector:")
|
||||||
|
print(matrix1)
|
||||||
|
|
||||||
|
# 2. Specifying row and column names at the time of creation
|
||||||
|
row_names <- c("Row1", "Row2", "Row3")
|
||||||
|
col_names <- c("Col1", "Col2", "Col3")
|
||||||
|
matrix2 <- matrix(1:9, nrow=3, ncol=3, dimnames=list(row_names, col_names))
|
||||||
|
print("Matrix with Row and Column Names:")
|
||||||
|
print(matrix2)
|
||||||
|
|
||||||
|
# 3. Creating a matrix by binding vectors column-wise using cbind()
|
||||||
|
vector1 <- 1:3
|
||||||
|
vector2 <- 4:6
|
||||||
|
vector3 <- 7:9
|
||||||
|
matrix3 <- cbind(vector1, vector2, vector3)
|
||||||
|
print("Column-Bound Matrix:")
|
||||||
|
print(matrix3)
|
||||||
|
|
||||||
|
# 4. Creating a matrix by binding vectors row-wise using rbind()
|
||||||
|
matrix4 <- rbind(vector1, vector2, vector3)
|
||||||
|
print("Row-Bound Matrix:")
|
||||||
|
print(matrix4)
|
||||||
|
|
||||||
|
# 5. Creating a diagonal matrix using diag()
|
||||||
|
diagonal_matrix <- diag(c(1, 2, 3))
|
||||||
|
print("Diagonal Matrix:")
|
||||||
|
print(diagonal_matrix)
|
||||||
|
|
||||||
|
# 6. Creating a matrix of zeros
|
||||||
|
zero_matrix <- matrix(0, nrow=3, ncol=3)
|
||||||
|
print("Zero Matrix:")
|
||||||
|
print(zero_matrix)
|
||||||
|
|
||||||
|
# 7. Creating a matrix of ones
|
||||||
|
one_matrix <- matrix(1, nrow=3, ncol=3)
|
||||||
|
print("One Matrix:")
|
||||||
|
print(one_matrix)
|
||||||
|
|
||||||
|
# 8. Creating an identity matrix
|
||||||
|
identity_matrix <- diag(1, 3)
|
||||||
|
print("Identity Matrix:")
|
||||||
|
print(identity_matrix)
|
||||||
|
|
||||||
|
# 9. Using outer to create a matrix from two vectors
|
||||||
|
matrix5 <- outer(1:3, 1:3, FUN="*")
|
||||||
|
print("Matrix from Outer Product:")
|
||||||
|
print(matrix5)
|
||||||
|
|
||||||
|
|
||||||
|
# 9.5: using the byrow
|
||||||
|
matrix6 <- matrix(1:9, nrow=3, ncol=3, byrow=TRUE)
|
||||||
|
print("Matrix by Row:")
|
||||||
|
print(matrix6)
|
||||||
|
|
||||||
|
# 10. Creating a matrix with random numbers
|
||||||
|
random_matrix <- matrix(runif(9), nrow=3)
|
||||||
|
print("Matrix with Random Numbers:")
|
||||||
|
print(random_matrix)
|
50
create_vectors.R
Normal file
50
create_vectors.R
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
# 1. Using the c() function to create a numeric vector
|
||||||
|
numeric_vector <- c(1, 2, 3, 4, 5)
|
||||||
|
print("Numeric Vector:") # Output: [1] "Numeric Vector:"
|
||||||
|
print(numeric_vector) # Output: [1] 1 2 3 4 5
|
||||||
|
|
||||||
|
# 2. Using the c() function to create a character vector
|
||||||
|
character_vector <- c("apple", "banana", "cherry")
|
||||||
|
print("Character Vector:") # Output: [1] "Character Vector:"
|
||||||
|
print(character_vector) # Output: [1] "apple" "banana" "cherry"
|
||||||
|
|
||||||
|
# 3. Using the c() function to create a logical vector
|
||||||
|
logical_vector <- c(TRUE, FALSE, TRUE, FALSE)
|
||||||
|
print("Logical Vector:") # Output: [1] "Logical Vector:"
|
||||||
|
print(logical_vector) # Output: [1] TRUE FALSE TRUE FALSE
|
||||||
|
|
||||||
|
# 4. Using the seq() function to create a sequence of numbers
|
||||||
|
sequence_vector <- seq(1, 10, by=2)
|
||||||
|
print("Sequence Vector (by 2):") # Output: [1] "Sequence Vector (by 2):"
|
||||||
|
print(sequence_vector) # Output: [1] 1 3 5 7 9
|
||||||
|
|
||||||
|
# 5. Using the rep() function to create a repeated vector
|
||||||
|
repeated_vector <- rep(x = 6, times = 4)
|
||||||
|
print("Repeated Vector:") # Output: [1] "Repeated Vector:"
|
||||||
|
print(repeated_vector) # Output: [1] 6 6 6 6
|
||||||
|
|
||||||
|
# 6. Using the : operator to create an integer sequence
|
||||||
|
colon_vector <- 1:10
|
||||||
|
print("Colon Operator Vector:") # Output: [1] "Colon Operator Vector:"
|
||||||
|
print(colon_vector) # Output: [1] 1 2 3 4 5 6 7 8 9 10
|
||||||
|
|
||||||
|
# 7. Using the seq_along() function on another vector
|
||||||
|
along_vector <- seq_along(c("x", "y", "z"))
|
||||||
|
print("Seq Along Vector:") # Output: [1] "Seq Along Vector:"
|
||||||
|
print(along_vector) # Output: [1] 1 2 3
|
||||||
|
|
||||||
|
# 8. Using the seq_len() function to generate a sequence of given length
|
||||||
|
length_vector <- seq_len(5)
|
||||||
|
print("Seq Length Vector:") # Output: [1] "Seq Length Vector:"
|
||||||
|
print(length_vector) # Output: [1] 1 2 3 4 5
|
||||||
|
|
||||||
|
# 9. Combining different types in a vector (coerced to the same type)
|
||||||
|
mixed_vector <- c(1, "two", TRUE)
|
||||||
|
print("Mixed Type Vector (coerced):") # Output: [1] "Mixed Type Vector (coerced):"
|
||||||
|
print(mixed_vector) # Output: [1] "1" "two" "TRUE"
|
||||||
|
|
||||||
|
# 10. Using the gl() function to generate factors
|
||||||
|
generated_vector <- gl(n = 3, k = 2, labels = c("Group1", "Group2", "Group3"))
|
||||||
|
print("Generated Factor Vector:") # Output: [1] "Generated Factor Vector:"
|
||||||
|
print(generated_vector) # Output: [1] Group1 Group1 Group2 Group2 Group3 Group3
|
||||||
|
# Levels: Group1 Group2 Group3
|
57
dataframes.R
Normal file
57
dataframes.R
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
# Creating a data frame
|
||||||
|
employee_data <- data.frame(
|
||||||
|
EmployeeID = c(1, 2, 3, 4),
|
||||||
|
Name = c("Alice", "Bob", "Charlie", "David"),
|
||||||
|
Age = c(28, 34, 29, 40),
|
||||||
|
Department = c("HR", "IT", "Marketing", "Finance"),
|
||||||
|
stringsAsFactors = FALSE
|
||||||
|
)
|
||||||
|
print("Original Data Frame:")
|
||||||
|
print(employee_data)
|
||||||
|
|
||||||
|
# Accessing data frame columns
|
||||||
|
print("Names Column:")
|
||||||
|
print(employee_data$Name)
|
||||||
|
|
||||||
|
# Accessing rows and columns using indices
|
||||||
|
print("Second Row, Third Column:")
|
||||||
|
print(employee_data[2, 3])
|
||||||
|
|
||||||
|
# Adding a new column
|
||||||
|
employee_data$Salary <- c(50000, 55000, 49000, 53000)
|
||||||
|
print("Data Frame with Salary Column:")
|
||||||
|
print(employee_data)
|
||||||
|
|
||||||
|
# Removing a column
|
||||||
|
employee_data$Age <- NULL
|
||||||
|
print("Data Frame after Removing Age Column:")
|
||||||
|
print(employee_data)
|
||||||
|
|
||||||
|
# Filtering rows
|
||||||
|
it_department <- subset(employee_data, Department == "IT")
|
||||||
|
print("Employees in IT Department:")
|
||||||
|
print(it_department)
|
||||||
|
|
||||||
|
# Summarizing data
|
||||||
|
average_salary <- mean(employee_data$Salary)
|
||||||
|
print(paste("Average Salary:", average_salary))
|
||||||
|
|
||||||
|
# Using dplyr for more advanced data frame manipulation
|
||||||
|
# Uncomment the next lines if dplyr is not installed
|
||||||
|
# install.packages("dplyr")
|
||||||
|
library(dplyr)
|
||||||
|
|
||||||
|
# Selecting specific columns with dplyr
|
||||||
|
selected_columns <- select(employee_data, Name, Salary)
|
||||||
|
print("Selected Columns:")
|
||||||
|
print(selected_columns)
|
||||||
|
|
||||||
|
# Filtering with dplyr
|
||||||
|
high_earners <- filter(employee_data, Salary > 50000)
|
||||||
|
print("High Earners:")
|
||||||
|
print(high_earners)
|
||||||
|
|
||||||
|
# Arranging rows by a column
|
||||||
|
sorted_employees <- arrange(employee_data, desc(Salary))
|
||||||
|
print("Employees Sorted by Salary:")
|
||||||
|
print(sorted_employees)
|
13
evoalgs-r-practise.Rproj
Normal file
13
evoalgs-r-practise.Rproj
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
Version: 1.0
|
||||||
|
|
||||||
|
RestoreWorkspace: Default
|
||||||
|
SaveWorkspace: Default
|
||||||
|
AlwaysSaveHistory: Default
|
||||||
|
|
||||||
|
EnableCodeIndexing: Yes
|
||||||
|
UseSpacesForTab: Yes
|
||||||
|
NumSpacesForTab: 2
|
||||||
|
Encoding: UTF-8
|
||||||
|
|
||||||
|
RnwWeave: Sweave
|
||||||
|
LaTeX: pdfLaTeX
|
65
functions.R
Normal file
65
functions.R
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
# Basic Function Definition
|
||||||
|
# -------------------------
|
||||||
|
# A simple function that adds two numbers
|
||||||
|
add_numbers <- function(x, y) {
|
||||||
|
sum <- x + y
|
||||||
|
return(sum)
|
||||||
|
}
|
||||||
|
|
||||||
|
# Using the function
|
||||||
|
result <- add_numbers(5, 3)
|
||||||
|
print(paste("Sum:", result))
|
||||||
|
|
||||||
|
# Function with Default Parameters
|
||||||
|
# --------------------------------
|
||||||
|
# A function with default values for parameters
|
||||||
|
multiply_numbers <- function(x, y = 2) {
|
||||||
|
product <- x * y
|
||||||
|
return(product)
|
||||||
|
}
|
||||||
|
|
||||||
|
# Using the function with default parameter
|
||||||
|
default_product <- multiply_numbers(4)
|
||||||
|
print(paste("Default Product:", default_product))
|
||||||
|
|
||||||
|
# Using the function by specifying both parameters
|
||||||
|
specified_product <- multiply_numbers(4, 3)
|
||||||
|
print(paste("Specified Product:", specified_product))
|
||||||
|
|
||||||
|
# Returning Multiple Values
|
||||||
|
# -------------------------
|
||||||
|
# A function that returns multiple values using a list
|
||||||
|
operate_numbers <- function(a, b) {
|
||||||
|
sum <- a + b
|
||||||
|
diff <- a - b
|
||||||
|
prod <- a * b
|
||||||
|
return(list(sum = sum, difference = diff, product = prod))
|
||||||
|
}
|
||||||
|
|
||||||
|
# Using the function
|
||||||
|
results <- operate_numbers(10, 5)
|
||||||
|
print("Operations Result:")
|
||||||
|
print(results)
|
||||||
|
|
||||||
|
# Variable Scope
|
||||||
|
# --------------
|
||||||
|
# A function to demonstrate variable scope
|
||||||
|
outer_function <- function() {
|
||||||
|
internal_var <- 20
|
||||||
|
inner_function <- function() {
|
||||||
|
print(paste("Accessing internal_var inside inner_function:", internal_var))
|
||||||
|
}
|
||||||
|
inner_function()
|
||||||
|
return(internal_var)
|
||||||
|
}
|
||||||
|
|
||||||
|
# Using the function
|
||||||
|
outer_result <- outer_function()
|
||||||
|
print(paste("Returned from outer_function:", outer_result))
|
||||||
|
|
||||||
|
# Anonymous Functions
|
||||||
|
# -------------------
|
||||||
|
# Using an anonymous function (a function without a name)
|
||||||
|
squared_numbers <- sapply(1:5, function(x) x^2)
|
||||||
|
print("Squared Numbers:")
|
||||||
|
print(squared_numbers)
|
27
if_else.R
Normal file
27
if_else.R
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
# Simple if-else statement
|
||||||
|
x <- 5
|
||||||
|
if (x > 0) {
|
||||||
|
print("x is positive")
|
||||||
|
} else {
|
||||||
|
print("x is non-positive")
|
||||||
|
}
|
||||||
|
|
||||||
|
# if-else-if ladder
|
||||||
|
score <- 85
|
||||||
|
if (score >= 90) {
|
||||||
|
print("Grade: A")
|
||||||
|
} else if (score >= 80) {
|
||||||
|
print("Grade: B")
|
||||||
|
} else if (score >= 70) {
|
||||||
|
print("Grade: C")
|
||||||
|
} else {
|
||||||
|
print("Grade: F")
|
||||||
|
}
|
||||||
|
|
||||||
|
# Vectorized ifelse statement
|
||||||
|
scores <- c(92, 81, 58, 77, 85)
|
||||||
|
grades <- ifelse(scores >= 90, "A",
|
||||||
|
ifelse(scores >= 80, "B",
|
||||||
|
ifelse(scores >= 70, "C", "F")))
|
||||||
|
print("Vectorized Grades:")
|
||||||
|
print(grades)
|
79
lists_and_vectors.R
Normal file
79
lists_and_vectors.R
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
# Vectors
|
||||||
|
# -------
|
||||||
|
# Creating a numeric vector
|
||||||
|
numeric_vector <- c(1, 2, 3, 4, 5)
|
||||||
|
print("Numeric Vector:")
|
||||||
|
print(numeric_vector)
|
||||||
|
|
||||||
|
# Creating a character vector
|
||||||
|
character_vector <- c("apple", "banana", "cherry")
|
||||||
|
print("Character Vector:")
|
||||||
|
print(character_vector)
|
||||||
|
|
||||||
|
# Accessing elements in a vector
|
||||||
|
print("Element at index 2 of Numeric Vector:")
|
||||||
|
print(numeric_vector[2])
|
||||||
|
|
||||||
|
# Modifying elements in a vector
|
||||||
|
numeric_vector[2] <- 10
|
||||||
|
print("Modified Numeric Vector:")
|
||||||
|
print(numeric_vector)
|
||||||
|
|
||||||
|
# Lists
|
||||||
|
# -----
|
||||||
|
# Creating a simple list
|
||||||
|
simple_list <- list(name="John", age=25, scores=c(88, 92, 85))
|
||||||
|
print("Simple List:")
|
||||||
|
print(simple_list)
|
||||||
|
|
||||||
|
# Accessing elements in a list by name
|
||||||
|
print("Name from List:")
|
||||||
|
print(simple_list$name)
|
||||||
|
|
||||||
|
# Accessing elements in a list by index
|
||||||
|
print("Scores from List:")
|
||||||
|
print(simple_list[[3]])
|
||||||
|
|
||||||
|
# Modifying elements in a list
|
||||||
|
simple_list$age <- 26
|
||||||
|
print("Modified List:")
|
||||||
|
print(simple_list)
|
||||||
|
|
||||||
|
# Differences between Lists and Vectors
|
||||||
|
# -------------------------------------
|
||||||
|
# Lists can contain elements of different types
|
||||||
|
mixed_list <- list(number=42, text="hello", vector=c(1, 2, 3))
|
||||||
|
print("Mixed Type List:")
|
||||||
|
print(mixed_list)
|
||||||
|
|
||||||
|
# Vectors must have elements of the same type
|
||||||
|
# Attempting to mix types will coerce to the most flexible type
|
||||||
|
mixed_vector <- c(1, "two", TRUE)
|
||||||
|
print("Mixed Type Vector (coerced):")
|
||||||
|
print(mixed_vector)
|
||||||
|
|
||||||
|
# Matrices
|
||||||
|
# --------
|
||||||
|
# Creating a matrix
|
||||||
|
matrix_example <- matrix(1:9, nrow = 3, ncol = 3)
|
||||||
|
print("Matrix Example:")
|
||||||
|
print(matrix_example)
|
||||||
|
|
||||||
|
# Accessing elements in a matrix
|
||||||
|
print("Element at row 2, column 3:")
|
||||||
|
print(matrix_example[2, 3])
|
||||||
|
|
||||||
|
# Modifying elements in a matrix
|
||||||
|
matrix_example[1, 2] <- 10
|
||||||
|
print("Modified Matrix:")
|
||||||
|
print(matrix_example)
|
||||||
|
|
||||||
|
# Creating a matrix with named columns
|
||||||
|
named_matrix <- matrix(1:9, nrow = 3, ncol = 3, dimnames = list(NULL, c("col1", "col2", "col3")))
|
||||||
|
print("Matrix with Named Columns:")
|
||||||
|
print(named_matrix)
|
||||||
|
|
||||||
|
# Accessing elements in a matrix with named columns using $ indexing
|
||||||
|
print("Element in 'col2':")
|
||||||
|
print(named_matrix[, "col2"])
|
||||||
|
|
33
loops.R
Normal file
33
loops.R
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
# For loop example
|
||||||
|
print("For Loop Output:")
|
||||||
|
for (i in 1:5) {
|
||||||
|
print(paste("Iteration", i))
|
||||||
|
}
|
||||||
|
|
||||||
|
# While loop example
|
||||||
|
print("While Loop Output:")
|
||||||
|
count <- 1
|
||||||
|
while (count <= 5) {
|
||||||
|
print(paste("Count is", count))
|
||||||
|
count <- count + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Repeat loop example with break
|
||||||
|
print("Repeat Loop Output (with break):")
|
||||||
|
count <- 1
|
||||||
|
repeat {
|
||||||
|
if (count > 5) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
print(paste("Repeat count is", count))
|
||||||
|
count <- count + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Using next to skip an iteration
|
||||||
|
print("For Loop with Next:")
|
||||||
|
for (i in 1:5) {
|
||||||
|
if (i == 3) {
|
||||||
|
next
|
||||||
|
}
|
||||||
|
print(paste("Iteration", i))
|
||||||
|
}
|
103
operators.R
Normal file
103
operators.R
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
# Addition
|
||||||
|
result <- 10 + 5
|
||||||
|
print(result) # 15
|
||||||
|
|
||||||
|
# Subtraction
|
||||||
|
result <- 10 - 5
|
||||||
|
print(result) # 5
|
||||||
|
|
||||||
|
# Multiplication
|
||||||
|
result <- 10 * 5
|
||||||
|
print(result) # 50
|
||||||
|
|
||||||
|
# Division
|
||||||
|
result <- 10 / 5
|
||||||
|
print(result) # 2
|
||||||
|
result <- 10 / 3
|
||||||
|
print(result) # 3.333333
|
||||||
|
|
||||||
|
# Exponentiation
|
||||||
|
result <- 10 ^ 2
|
||||||
|
print(result) # 100
|
||||||
|
|
||||||
|
# Integer Division
|
||||||
|
result <- 10 %/% 3
|
||||||
|
print(result) # 3
|
||||||
|
|
||||||
|
# Modulus
|
||||||
|
result <- 10 %% 3
|
||||||
|
print(result) # 1
|
||||||
|
|
||||||
|
|
||||||
|
# AND
|
||||||
|
result <- TRUE & FALSE
|
||||||
|
print(result) # FALSE
|
||||||
|
|
||||||
|
# OR
|
||||||
|
result <- TRUE | FALSE
|
||||||
|
print(result) # TRUE
|
||||||
|
|
||||||
|
# NOT
|
||||||
|
result <- !TRUE
|
||||||
|
print(result) # FALSE
|
||||||
|
|
||||||
|
|
||||||
|
# Greater than
|
||||||
|
result <- 10 > 5
|
||||||
|
print(result) # TRUE
|
||||||
|
|
||||||
|
# Less than
|
||||||
|
result <- 10 < 5
|
||||||
|
print(result) # FALSE
|
||||||
|
|
||||||
|
# Equal to
|
||||||
|
result <- 10 == 10
|
||||||
|
print(result) # TRUE
|
||||||
|
|
||||||
|
# Not equal to
|
||||||
|
result <- 10 != 5
|
||||||
|
print(result) # TRUE
|
||||||
|
|
||||||
|
# Greater than or equal to
|
||||||
|
result <- 10 >= 10
|
||||||
|
print(result) # TRUE
|
||||||
|
|
||||||
|
# Less than or equal to
|
||||||
|
result <- 10 <= 5
|
||||||
|
print(result) # FALSE
|
||||||
|
|
||||||
|
|
||||||
|
# Basic assignment
|
||||||
|
x <- 10
|
||||||
|
print(x) # 10
|
||||||
|
|
||||||
|
# Alternate assignment (less common in R)
|
||||||
|
x = 20
|
||||||
|
print(x) # 20
|
||||||
|
|
||||||
|
# Increment and assign (not directly supported like in other languages, shown with equivalent R code)
|
||||||
|
x <- x + 5
|
||||||
|
print(x) # 25
|
||||||
|
|
||||||
|
# Decrement and assign (equivalent R code)
|
||||||
|
x <- x - 5
|
||||||
|
print(x) # 20
|
||||||
|
|
||||||
|
# Multiply and assign (equivalent R code)
|
||||||
|
x <- x * 2
|
||||||
|
print(x) # 40
|
||||||
|
|
||||||
|
# Divide and assign (equivalent R code)
|
||||||
|
x <- x / 2
|
||||||
|
print(x) # 20
|
||||||
|
|
||||||
|
|
||||||
|
# Element-wise multiplication of vectors
|
||||||
|
v1 <- c(1, 2, 3)
|
||||||
|
v2 <- c(4, 5, 6)
|
||||||
|
result <- v1 * v2
|
||||||
|
print(result) # c(4, 10, 18)
|
||||||
|
|
||||||
|
# Element-wise comparison of vectors
|
||||||
|
result <- v1 > 2
|
||||||
|
print(result) # c(FALSE, FALSE, TRUE)
|
47
simple_plotting.R
Normal file
47
simple_plotting.R
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
# If you don't have the ggplot2 package installed, you can install it using:
|
||||||
|
# install.packages("ggplot2")
|
||||||
|
|
||||||
|
# Load necessary library
|
||||||
|
library(ggplot2)
|
||||||
|
|
||||||
|
# Creating some data for plotting
|
||||||
|
x <- 1:10
|
||||||
|
y <- x^2
|
||||||
|
|
||||||
|
# Base R Plotting
|
||||||
|
# ---------------
|
||||||
|
|
||||||
|
# Scatter Plot using base R
|
||||||
|
plot(x, y, main="Scatter Plot", xlab="X Axis", ylab="Y Axis", col="blue", pch=19)
|
||||||
|
|
||||||
|
# Line Plot using base R
|
||||||
|
plot(x, y, type="l", main="Line Plot", xlab="X Axis", ylab="Y Axis", col="red")
|
||||||
|
|
||||||
|
# Histogram using base R
|
||||||
|
hist(y, main="Histogram", xlab="Values", col="green", breaks=5)
|
||||||
|
|
||||||
|
# Advanced Plotting with ggplot2
|
||||||
|
# ------------------------------
|
||||||
|
|
||||||
|
# Data frame for ggplot2
|
||||||
|
data <- data.frame(x, y)
|
||||||
|
|
||||||
|
# Scatter Plot using ggplot2
|
||||||
|
ggplot(data, aes(x=x, y=y)) +
|
||||||
|
geom_point() +
|
||||||
|
ggtitle("Scatter Plot with ggplot2") +
|
||||||
|
xlab("X Axis") +
|
||||||
|
ylab("Y Axis")
|
||||||
|
|
||||||
|
# Line Plot using ggplot2
|
||||||
|
ggplot(data, aes(x=x, y=y)) +
|
||||||
|
geom_line(color="red") +
|
||||||
|
ggtitle("Line Plot with ggplot2") +
|
||||||
|
xlab("X Axis") +
|
||||||
|
ylab("Y Axis")
|
||||||
|
|
||||||
|
# Histogram using ggplot2
|
||||||
|
ggplot(data, aes(x=y)) +
|
||||||
|
geom_histogram(bins=5, fill="green", color="black") +
|
||||||
|
ggtitle("Histogram with ggplot2") +
|
||||||
|
xlab("Values")
|
19
testfile-1.R
Normal file
19
testfile-1.R
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
# Load necessary package
|
||||||
|
library(ggplot2)
|
||||||
|
|
||||||
|
# Create some data
|
||||||
|
df <- data.frame(
|
||||||
|
x = 1:100,
|
||||||
|
y = 2 * (1:100) + rnorm(100)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Perform linear regression
|
||||||
|
model <- lm(y ~ x, data = df)
|
||||||
|
|
||||||
|
# Summary of the model
|
||||||
|
summary(model)
|
||||||
|
|
||||||
|
# Plot the data and the model
|
||||||
|
ggplot(df, aes(x = x, y = y)) +
|
||||||
|
geom_point() +
|
||||||
|
geom_smooth(method = "lm", col = "red")
|
62
vectors_and_matrices.R
Normal file
62
vectors_and_matrices.R
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
# Create vectors
|
||||||
|
vector1 <- c(1, 2, 3)
|
||||||
|
vector2 <- c(4, 5, 6)
|
||||||
|
|
||||||
|
# Basic Vector Operations
|
||||||
|
# -----------------------
|
||||||
|
# Element-wise addition
|
||||||
|
vector_add <- vector1 + vector2
|
||||||
|
print("Vector Addition:")
|
||||||
|
print(vector_add)
|
||||||
|
|
||||||
|
# Element-wise multiplication
|
||||||
|
vector_mult <- vector1 * vector2
|
||||||
|
print("Vector Multiplication:")
|
||||||
|
print(vector_mult)
|
||||||
|
|
||||||
|
# Dot product
|
||||||
|
dot_product <- sum(vector1 * vector2)
|
||||||
|
print("Dot Product:")
|
||||||
|
print(dot_product)
|
||||||
|
|
||||||
|
# Create Matrices
|
||||||
|
# ---------------
|
||||||
|
# Matrix from combining vectors as rows
|
||||||
|
matrix1 <- rbind(vector1, vector2)
|
||||||
|
print("Matrix from Vectors (Rows):")
|
||||||
|
print(matrix1)
|
||||||
|
|
||||||
|
# Matrix from combining vectors as columns
|
||||||
|
matrix2 <- cbind(vector1, vector2)
|
||||||
|
print("Matrix from Vectors (Columns):")
|
||||||
|
print(matrix2)
|
||||||
|
|
||||||
|
# Matrix and Vector Operations
|
||||||
|
# ----------------------------
|
||||||
|
# Adding a vector to each row of the matrix (by replicating the vector)
|
||||||
|
matrix_row_add <- matrix1 + vector1
|
||||||
|
print("Add Vector to Each Row of Matrix:")
|
||||||
|
print(matrix_row_add)
|
||||||
|
|
||||||
|
# Adding a vector to each column of the matrix (by replicating the vector)
|
||||||
|
matrix_col_add <- matrix2 + cbind(vector1)
|
||||||
|
print("Add Vector to Each Column of Matrix:")
|
||||||
|
print(matrix_col_add)
|
||||||
|
|
||||||
|
# Element-wise multiplication of a matrix by a vector
|
||||||
|
# Note: This operation requires conformable dimensions
|
||||||
|
matrix_element_mult <- matrix1 * cbind(vector1)
|
||||||
|
print("Element-wise Multiplication of Matrix by Vector:")
|
||||||
|
print(matrix_element_mult)
|
||||||
|
|
||||||
|
# Matrix multiplication by a vector
|
||||||
|
# This treats the vector as a column vector
|
||||||
|
matrix_vector_mult <- matrix1 %*% vector1
|
||||||
|
print("Matrix Multiplication by Vector:")
|
||||||
|
print(matrix_vector_mult)
|
||||||
|
|
||||||
|
# Transpose a matrix and multiply by a vector
|
||||||
|
# This treats the vector as a row vector
|
||||||
|
transpose_mult <- t(matrix1) %*% vector1
|
||||||
|
print("Transpose Matrix and Multiply by Vector:")
|
||||||
|
print(transpose_mult)
|
Loading…
Reference in New Issue
Block a user