This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
a <- 32 b <- 17 addition_result <- a + b cat(“Addition Result:”, addition_result, “”) subtraction_result <- a - b cat(“Subtraction Result:”, subtraction_result, “”) multiplication_result <- a * b cat(“Multiplication Result:”, multiplication_result, “”) division_result <- a / b cat(“Division Result:”, division_result, “”)
num1 <- 32 num2 <- 17 my_prod <- num1 * num2 cat(“The product is:”, my_prod, “”)
100_variable
Variables can’t begin with a number or special character (except for a dot or underscore).
descriptives <- function(data) {mean_val <- mean(data) min_val <- min(data) max_val <- max(data) range_val <- range(data) result <- c(mean_val, min_val, max_val, range_val) return(result)} output <- descriptives(c(1, 2, 3, 4, 5)) cat(output, “”)
my_sequence <- seq(from = 58, to = 116, by = 2) print(my_sequence)
vector <- c(“Red”, “Yellow”, “Green”, “Blue”) print(vector)
vector <- c(TRUE, FALSE, 1, 0, NA, TRUE, FALSE) print(vector)
test_vector <- c(1.2, 4.5, 9.9) vector_type <- class(test_vector) print(vector_type)
heroes <- c(“Luke”, “Han”, “Leia”, “Lando”) leia <- heroes[3] print(leia)
heroes <- c(“Luke”, “Han”, “Leia”, “Lando”) vector_length <- length(heroes) print(vector_length)
primes <- c(1, 2, 3, 5, 7, 11, 13, 17, 19, 23) my_subset <- primes[c(2, 7)] print(my_subset)
q11_values <- c(1, 2, 3, 4, 5, 6, 7, 8, 9) greater_than_5 <- q11_values[q11_values > 5] print(greater_than_5)
my_list <- list( character_vector = c(“Red”, “Yellow”, “Green”), single_logical = TRUE, nested_numeric_list = list( c(1.1, 2.2), c(3.3, 4.4), c(5.5, 6.6))) print(my_list)
q13_matrix <- matrix(c(92, 103, 56, 89, 42, 5, 13, 17, 21), nrow = 3) selected_values <- q13_matrix[3, c(2, 3)] print(selected_values)
my_df <- data.frame( numbers = c(1, 2, 3, 4, 5), strings = c(“Red”, “Yellow”, “Green”, “Blue”, “Purple”), true_false = c(TRUE, FALSE, TRUE, FALSE, TRUE) ) print(my_df)
numbers <- c(1, 2, 3, 4, 5) strings <- c(“Red”, “Yellow”, “Green”) true_false <- c(TRUE, FALSE, TRUE, FALSE, TRUE) my_df <- data.frame(numbers, strings, true_false) # Error occurs due to the lengths of the vectors; all vectors used as columns must have the same length.
R will prioritize the data, with the values in positions 4, 5, and 6 being converted to character strings.
column1 <- q13_matrix[, 1] print(column1)
if (!requireNamespace(“tidyverse”, quietly = TRUE)) { install.packages(“tidyverse”) } library(tidyverse)
movie_data <- data.frame( movies = c(“The Godfather”, “L.A. Confidential”, “Gattaca”, “Michael Clayton”, “Inception”), star_review = c(1, 2, 3, 4, 5) ) highly_rated_movies <- movie_data[movie_data$star_review >= 3, “movies”] print(highly_rated_movies)
get_highly_rated_movies <- function(movie_data) { highly_rated_movies <- movie_data[movie_data$star_review >= 3, “movies”] return(highly_rated_movies)} movie_data <- data.frame( movies = c(“The Godfather”, “L.A. Confidential”, “Gattaca”, “Michael Clayton”, “Inception”), star_review = c(1, 2, 3, 4, 5)) highly_rated <- get_highly_rated_movies(movie_data) print(highly_rated)