Welcome to the basics of R programming! R is a powerful language for data analysis and statistical computing. In this guide, we'll cover the fundamentals to get you started on your journey with R.
Table of Contents
Installation
Before you start, make sure to install R and RStudio. R is the language, while RStudio is an integrated development environment (IDE) that provides a user-friendly interface.
Basic Syntax
R uses a command-line interface. Here's an example of a simple R script:
# Print "Hello, World!"
print("Hello, World!")
Data Types
R has several data types, including vectors, matrices, lists, and data frames. Here's an example of creating a vector:
# Create a vector
vector <- c(1, 2, 3, 4, 5)
Control Structures
Control structures allow you to execute code based on conditions. Here's an example of an if-else statement:
# If-else statement
if (1 > 0) {
print("True")
} else {
print("False")
}
Functions
Functions are blocks of code that perform a specific task. Here's an example of creating a custom function:
# Custom function
add_two_numbers <- function(x, y) {
return(x + y)
}
# Call the function
result <- add_two_numbers(3, 4)
print(result)
Further Reading
For more in-depth learning, check out the following resources: