💾 Archived View for gem.sdf.org › s.kaplan › cheatsheets › programming-languages › r.md captured on 2024-06-16 at 12:46:51.
⬅️ Previous capture (2023-09-28)
-=-=-=-=-=-=-
## R Cheatsheet ### Overview R is a programming language and environment for statistical computing and graphics. It is widely used in data analysis, machine learning, and scientific research. R was created by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand in 1993. ### Variables R variables can store a variety of data types, including numeric, character, logical, and complex. Variables are assigned using the `<-` or `=` operator.
x <- 3.14
name <- "Alice"
is_female <- TRUE
z <- 1 + 2i
### Functions R has a large number of built-in functions for common tasks such as data manipulation, statistical analysis, and plotting. Functions are called by name, with arguments in parentheses.
mean(c(1, 2, 3, 4, 5)) # Returns 3
t.test(c(1, 2, 3, 4, 5), mu=3) # One-sample t-test
plot(c(1, 2, 3, 4, 5), c(1, 4, 9, 16, 25), type="l")
### Loops R has several types of loops, including `for`, `while`, and `repeat`. The `for` loop is used to iterate over a sequence of values, while the `while` and `repeat` loops are used to repeat a block of code while a condition is true or false.
for (i in 1:10) {
print(i)
}
i <- 1
while (i <= 10) {
print(i)
i <- i + 1
}
i <- 1
repeat {
print(i)
i <- i + 1
if (i > 10) {
break
}
}
### Conditionals R has several conditional statements, including `if`, `else if`, and `else`. These statements are used to control the flow of a program based on certain conditions.
age <- 30
if (age >= 18) {
print("You are an adult")
}
age <- 15
if (age >= 18) {
print("You are an adult")
} else {
print("You are a minor")
}
age <- 25
if (age < 18) {
print("You are a minor")
} else if (age < 65) {
print("You are an adult")
} else {
print("You are a senior")
}
### File Manipulation R provides several functions for manipulating files, including `read.csv`, `write.csv`, `file.rename`, and `file.remove`.
data <- read.csv("data.csv")
write.csv(data, "data_new.csv")
file.rename("data.csv", "data_old.csv")
file.remove("data_old.csv")
### Resources - [R documentation](https://www.r-project.org/documentation/) - [RStudio](https://www.rstudio.com/) (integrated development environment) - [CRAN](https://cran.r-project.org/) (community repository) - [R for Data Science](https://r4ds.had.co.nz/) (online book)