Welcome to the hands-on session for learning R! In this activity, we will:
Let’s start with a simple command to familiarize yourself with the R console:
# Run this command in your console
greeting <- "Hello, R!"
cat(greeting)
## Hello, R!
cat()
function do?# Basic arithmetic
x <- 10
y <- 5
sum <- x + y
product <- x * y
sum
## [1] 15
product
## [1] 50
# Creating a vector
numbers <- c(1, 2, 3, 4, 5)
numbers
## [1] 1 2 3 4 5
You can use numeric indices to extract specific elements from a vector, and negative indices allow you to exclude specific elements. Logical indexing involves using a logical vector (TRUE or FALSE values) to subset elements that satisfy a condition. The which() function returns the indices of elements that meet a condition, which can then be used for subsetting. Additionally, head() and tail() functions can be used to extract the first or last n elements from a vector.
# 1. Indexing by Position (Numeric Indexing)
numbers[1:3]
## [1] 1 2 3
# 2. Negative Indexing
numbers[-2] # Removes the second element
## [1] 1 3 4 5
numbers[-(1:3)] # Removes the first three elements
## [1] 4 5
# 3. Logical Indexing
numbers[numbers %% 2 == 0] # Extracts even numbers
## [1] 2 4
# 4. Using the which() Function
numbers[which(numbers > 3)] # Extracts numbers greater than 3
## [1] 4 5
# 5. Using head() and tail()
head(numbers, 3) # Extracts the first 3 elements
## [1] 1 2 3
tail(numbers, 3) # Extracts the last 3 elements
## [1] 3 4 5
# Creating a data frame
data <- data.frame(
Name = c("Alice", "Bob", "Charlie"),
Age = c(25, 30, 35),
Score = c(90, 85, 88)
)
data
## Name Age Score
## 1 Alice 25 90
## 2 Bob 30 85
## 3 Charlie 35 88
# Access specific columns
data$Name
## [1] "Alice" "Bob" "Charlie"
# Access specific rows
data[1, ]
## Name Age Score
## 1 Alice 25 90
# Adding a new column
data$Passed <- data$Score > 80
data
## Name Age Score Passed
## 1 Alice 25 90 TRUE
## 2 Bob 30 85 TRUE
## 3 Charlie 35 88 TRUE
# Logical comparisons
x <- 10
y <- 5
x > y
## [1] TRUE
x == y
## [1] FALSE
# Using if-else
if (x > y) {
message("x is greater than y")
} else {
message("x is not greater than y")
}
## x is greater than y
# Example of a for loop
for (i in 1:10) {
print(i)
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
# Example of a while loop
i <- 1
while (i <= 10) {
print(i)
i <- i + 1
# if (i > 5) { # Break the loop when x exceeds 5
# break
# }
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
# Defining a function
add_numbers <- function(a, b) {
return(a + b)
}
result <- add_numbers(10, 5)
result
## [1] 15
# Function with conditional logic
evaluate_score <- function(score) {
if (score >= 50) {
return("Pass")
} else {
return("Fail")
}
}
evaluate_score(75)
## [1] "Pass"
evaluate_score(45)
## [1] "Fail"
# Built-in dataset
x <- 1:10
y <- x^2
plot(x, y, main="Scatter Plot", xlab="X-axis", ylab="Y-axis", col="blue")
# Using ggplot2 for enhanced visualization
library(ggplot2)
data(mpg)
ggplot(data = mpg, aes(x = displ, y = hwy, color = class)) +
geom_point(size = 3) +
labs(title = "Engine Displacement vs. Highway MPG",
x = "Engine Displacement (L)",
y = "Highway MPG") +
theme_minimal()
ggplot(data = mpg, aes(x = displ, y = hwy, color = class)) +
geom_point(size = 3) +
labs(title = "Engine Displacement vs. Highway MPG",
x = "Engine Displacement (L)",
y = "Highway MPG") +
scale_color_manual(values = c("compact" = "blue", "midsize" = "green", "suv" = "red", "pickup" = "orange", "minivan" = "purple")) +
theme_minimal()
Using scale_color_brewer() for predefined color palettes.
ggplot(data = mpg, aes(x = displ, y = hwy, color = class)) +
geom_point(size = 3) +
labs(title = "Engine Displacement vs. Highway MPG",
x = "Engine Displacement (L)",
y = "Highway MPG") +
scale_color_brewer(palette = "Set1") + # Choose a predefined color palette
theme_minimal()
Helpful Chunk Options:
- out.width
: Controls the output width of
the plot in the rendered document (useful for resizing the plot after it
is generated). out.width=“50%” will make the plot width 50% of the
available width in the output document.
- fig.width
: Controls the width of the
plot (in inches).
- **fig.height**
: Controls the height of the plot (in
inches).
ggplot(data = mpg, aes(x = displ, y = hwy, color = class)) +
geom_point(size = 3) +
labs(title = "Engine Displacement vs. Highway MPG",
x = "Engine Displacement (L)",
y = "Highway MPG") +
scale_color_brewer(palette = "Set1") + # Choose a predefined color palette
theme_minimal()
To view the full list of available palettes and how they look, you can use the RColorBrewer package directly:
library(RColorBrewer)
display.brewer.all()
ggplot(data = mpg, aes(x = displ, y = hwy, color = class)) +
geom_point(size = 3) +
geom_smooth(method = "lm") +
labs(title = "Engine Displacement vs. Highway MPG",
x = "Engine Displacement (L)",
y = "Highway MPG") +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
r, followed by an optional chunk name, followed by comma-seprated
options 1. easy navigation between chunks using the drop-down code
navigator
2. setup cached chunks to avoid rerun
60+ customized options, arguments supplied to the chunk header.
Learn more about chunk options at
eval = FALSE prevents code from being evaluated.
include = FALSE runs the code, but doesn’t show the code or results in
the final document.
echo = FALSE prevents code, but not the results from appearing in the
finished file.
message = FALSE or warning = FALSE prevents messages or warnings from
appearing in the finished file.
results = ‘hide’ hides printed output, fig.show = ‘hide’ hides
plots.
error = TRUE causes the render to continue even if code returns an
error.
print("hello world!")
## [1] "hello world!"
## [1] "hello world!"
By default, R Markdown prints data frames and matrices as you’d see them in the console:
mtcars[1:5, ]
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
If you prefer that data be displayed with additional formatting you can use the knitr::kable function. The code below generates Table 27.1.
knitr::kable(
mtcars[1:5, ],
caption = "A knitr kable."
)
mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | |
---|---|---|---|---|---|---|---|---|---|---|---|
Mazda RX4 | 21.0 | 6 | 160 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 |
Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 |
Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 | 3.440 | 17.02 | 0 | 0 | 3 | 2 |
You can cite the references in your document using the @ symbol
followed by the citation key.
you can add a citation [@ZANTIS2023161211].