From 25f0531dcd1ebd2633d2d22e7631d5f2ae0f8431 Mon Sep 17 00:00:00 2001 From: Yandrik Date: Tue, 30 Apr 2024 10:03:37 +0200 Subject: [PATCH] INITIAL COMMIT --- .gitignore | 4 ++ Rplots.pdf | Bin 0 -> 9846 bytes Rplots1.pdf | Bin 0 -> 4780 bytes apply.R | 53 ++++++++++++++++++++ classes.R | 42 ++++++++++++++++ create_lists.R | 58 ++++++++++++++++++++++ create_matrices.R | 60 +++++++++++++++++++++++ create_vectors.R | 50 +++++++++++++++++++ dataframes.R | 57 ++++++++++++++++++++++ evoalgs-r-practise.Rproj | 13 +++++ functions.R | 65 ++++++++++++++++++++++++ if_else.R | 27 ++++++++++ lists_and_vectors.R | 79 ++++++++++++++++++++++++++++++ loops.R | 33 +++++++++++++ operators.R | 103 +++++++++++++++++++++++++++++++++++++++ simple_plotting.R | 47 ++++++++++++++++++ testfile-1.R | 19 ++++++++ vectors_and_matrices.R | 62 +++++++++++++++++++++++ 18 files changed, 772 insertions(+) create mode 100644 .gitignore create mode 100644 Rplots.pdf create mode 100644 Rplots1.pdf create mode 100644 apply.R create mode 100644 classes.R create mode 100644 create_lists.R create mode 100644 create_matrices.R create mode 100644 create_vectors.R create mode 100644 dataframes.R create mode 100644 evoalgs-r-practise.Rproj create mode 100644 functions.R create mode 100644 if_else.R create mode 100644 lists_and_vectors.R create mode 100644 loops.R create mode 100644 operators.R create mode 100644 simple_plotting.R create mode 100644 testfile-1.R create mode 100644 vectors_and_matrices.R diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5b6a065 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.Rproj.user +.Rhistory +.RData +.Ruserdata diff --git a/Rplots.pdf b/Rplots.pdf new file mode 100644 index 0000000000000000000000000000000000000000..8d33e91007edfd5d2f552cc9c84e8d5a3b548a3d GIT binary patch literal 9846 zcmb7K2UJsAvqmW@AV>#kQRxaHp*KM~(rf4tLg)}6L3*!(qKNd~n}A3O9T5@fz4sz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h7`es75zLXH zd#xoZFCSYltKN-u9&{EfW-lJXW7qZ9rHC&^yK7zq_QXu{yt7y$zp?378mV~Y+txin zy!b{cv@G<>55*sXd+hg&g}o2UKhyH;dU&w<{kzQSuhcmrIwQ0j)I`1XTwB+#)T|zy zWt;WA7||2fvy}Dqc|v3A#bA*nRt~me*3iQ!9y89~r(xBuFC$W!i^b~1+QpW&(zMRr z3&`fn=DBw}+oi>?C8b3VU3AW-**UI1mgfbv1wE5Jb9*|Ee^X|x%1gE3R2Fh-Q*1`t zKVD8|&^En(V}rX%B3(k4yY~|8lI*2r<*am;uC>0foN|M_O8iH;k31Rsi2I0Fm)jt- zCgN4cuS=@w`xs4RsHRP4m?wX>e0Io^EhBF;Z!o9cn9XQ3b6ra9c%2&4jpUo&mU>*d ztXuGM`BivKX-8>X>HYTf8%vIg5+H5`jOIxEKtz0;MD7I*Wu9qdZ@;~ruf6B;eP8p zD;xW-gksm_&VZ+zT`bwralto(mA8Zyb(GdaJ2$JV&SvMA%{PWjh0KS?G6i=gb#}_$ zlEoBx72cCq~f4muANX)CaAQKqk2r>#JZ0;(LkH0-$zkLG9#nas(p94dV_#NV`FzX`MeF zkNOAhK_y^OB&sJKMWp}`B9=(NT%dVF?OG<&J9nplY#(-e{hy8apX-O+NdRI&JxAHy zM$kdph+&NKq=iJA4tDeMr;Hv3?}MS>&?pe(XI=n$M*q33@FQW|K~Mk&13`a%fTDte zyaM13{KDY0oecd02){6xB5e-(Zwv;9(gurvV@TQp<6jtpX6ZLf{x^G2IE=P^`EOk) zT=_Q)0fYbM10j$6M=TT)3Wvv#Xe%WEVu1_5(B2C`&Jx^-wCRkV4~RLDNCD`pp55G# VDJT+UH!}zX5)P7(&^FNl{Rg66DY5_n literal 0 HcmV?d00001 diff --git a/apply.R b/apply.R new file mode 100644 index 0000000..4e7bf74 --- /dev/null +++ b/apply.R @@ -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) diff --git a/classes.R b/classes.R new file mode 100644 index 0000000..7e6a5c5 --- /dev/null +++ b/classes.R @@ -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) \ No newline at end of file diff --git a/create_lists.R b/create_lists.R new file mode 100644 index 0000000..d78d9e6 --- /dev/null +++ b/create_lists.R @@ -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) \ No newline at end of file diff --git a/create_matrices.R b/create_matrices.R new file mode 100644 index 0000000..5be294d --- /dev/null +++ b/create_matrices.R @@ -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) \ No newline at end of file diff --git a/create_vectors.R b/create_vectors.R new file mode 100644 index 0000000..75e5f91 --- /dev/null +++ b/create_vectors.R @@ -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 diff --git a/dataframes.R b/dataframes.R new file mode 100644 index 0000000..98b8a7d --- /dev/null +++ b/dataframes.R @@ -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) diff --git a/evoalgs-r-practise.Rproj b/evoalgs-r-practise.Rproj new file mode 100644 index 0000000..8e3c2eb --- /dev/null +++ b/evoalgs-r-practise.Rproj @@ -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 diff --git a/functions.R b/functions.R new file mode 100644 index 0000000..b4f17c0 --- /dev/null +++ b/functions.R @@ -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) \ No newline at end of file diff --git a/if_else.R b/if_else.R new file mode 100644 index 0000000..db9a251 --- /dev/null +++ b/if_else.R @@ -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) \ No newline at end of file diff --git a/lists_and_vectors.R b/lists_and_vectors.R new file mode 100644 index 0000000..c3708af --- /dev/null +++ b/lists_and_vectors.R @@ -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"]) + diff --git a/loops.R b/loops.R new file mode 100644 index 0000000..938e95c --- /dev/null +++ b/loops.R @@ -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)) +} \ No newline at end of file diff --git a/operators.R b/operators.R new file mode 100644 index 0000000..056f393 --- /dev/null +++ b/operators.R @@ -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) diff --git a/simple_plotting.R b/simple_plotting.R new file mode 100644 index 0000000..90da851 --- /dev/null +++ b/simple_plotting.R @@ -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") diff --git a/testfile-1.R b/testfile-1.R new file mode 100644 index 0000000..07d6b16 --- /dev/null +++ b/testfile-1.R @@ -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") diff --git a/vectors_and_matrices.R b/vectors_and_matrices.R new file mode 100644 index 0000000..6863d30 --- /dev/null +++ b/vectors_and_matrices.R @@ -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) \ No newline at end of file