Are you excited to continue your journey in data science with R? In this section, we'll revisit some of the foundational R programming concepts you've already encountered, ensuring you're well-prepared for more advanced topics. Our focus will be on data structures, basic operations, control structures, and custom functions in R.
We'll cover the following key concepts in this lesson:
if-else
), loops (for
, while
), and custom functions in R.Let's start by refreshing our knowledge of different data structures in R: vectors, matrices, lists, and data frames.
Vectors are one-dimensional arrays that can hold numeric, character, or logical data types.
R1nums <- c(1, 2, 3, 4, 5) 2print(nums)
In this example, nums
is a numeric vector containing the numbers 1 through 5.
Matrices are two-dimensional arrays that hold elements of the same data type.
R1matrix_example <- matrix(1:9, nrow = 3, byrow = TRUE) 2print(matrix_example)
Here, matrix_example
is a 3x3 matrix filled by rows with numbers from 1 to 9.
Lists in R can hold elements of different types, including other lists.
R1list_example <- list(nums, matrix_example, c("a", "b", "c")) 2print(list_example)
In this example, list_example
contains a vector, a matrix, and a character vector.
Data frames are two-dimensional tables where each column can hold different types of data.
R1df <- data.frame(ID = 1:3, Name = c("Alice", "Bob", "Charlie"), Score = c(9.5, 8.7, 9.8)) 2print(df)
Here, df
is a data frame with three columns: ID
, Name
, and Score
.
Now, let's revisit basic operations such as arithmetic operations, calculating mean and sum, and type conversions.
Summing the elements of a vector:
R1sum_example <- sum(nums) 2print(sum_example)
In this case, sum_example
will be the sum of elements in nums
, which is 15.
Calculating the mean (average) of a vector:
R1mean_example <- mean(nums) 2print(mean_example)
mean_example
will hold the mean value of the elements in nums
, which is 3.
Converting a numeric vector to character:
R1converted_nums <- as.character(nums) 2print(converted_nums)
Here, converted_nums
will be a character vector with elements "1"
, "2"
, "3"
, "4"
, and "5"
.
Control structures help us define the flow of execution in R scripts. We'll cover conditionals (if-else
), loops (for
, while
), and custom functions.
Conditionals allow us to execute certain code blocks based on the value of logical expressions.
R1x <- 5 2if (x > 0) { 3 cat("x is positive\n") 4} else { 5 cat("x is non-positive\n") 6}
In this example, since x
is greater than 0, the output will be "x is positive".
For loops iterate over a sequence of values.
R1for (i in nums) { 2 cat(sprintf("Value: %d\n", i)) 3}
This loop will print each value in nums
.
While loops execute as long as a condition is true.
R1i <- 1 2while (i <= 3) { 3 cat(sprintf("Iteration: %d\n", i)) 4 i <- i + 1 5}
This while loop will print "Iteration: 1", "Iteration: 2", and "Iteration: 3".
Creating custom functions allows us to encapsulate code logic and reuse it.
R1custom_function <- function(a, b) { 2 return(a + b) 3} 4result <- custom_function(3, 4) 5cat(sprintf("Custom Function Result: %d\n", result))
In this example, custom_function
takes two arguments and returns their sum. The result
will be 7.
A solid grasp of these essential R programming concepts is crucial for any data science endeavor. Understanding data structures, basic operations, and control structures will empower you to handle data more effectively and write efficient code. Custom functions, in particular, allow you to encapsulate logic and reuse it, making your code cleaner and more modular.
Relearning these foundational skills will ensure you're equipped to tackle more complex problems and data manipulation tasks later in the course. So let's make sure this groundwork is rock-solid.
Are you ready to dive back in and refresh these core concepts? Let's get to it!