Introduction to R
R is a programming language and environment for statistical computing and graphics. It is widely used for data analysis, machine learning, and data visualization. R provides a rich ecosystem of packages for various statistical and graphical techniques.
Key Features of R:
- Statistical Computing: R is designed for statistical analysis and data manipulation.
- Data Visualization: R provides powerful tools for creating graphs and plots.
- Extensive Libraries: R has a vast collection of packages for various tasks.
- Open Source: R is free to use and has a large community of contributors.
Example: Hello World in R
# R Hello World Program
print("Hello, World!")
This program uses the print
function to display "Hello, World!" on the screen.
Installing R
To get started with R, you need to install it on your computer. You can download R from the official website: CRAN (Comprehensive R Archive Network).
RStudio
RStudio is an integrated development environment (IDE) for R. It provides a user-friendly interface for writing and executing R code. You can download RStudio from: RStudio Download.
Basic R Syntax
R syntax is straightforward and easy to learn. Here are some basic examples:
# Assigning values to variables
x <- 10
y <- 20
# Performing arithmetic operations
sum <- x + y
print(sum)
# Creating a vector
numbers <- c(1, 2, 3, 4, 5)
print(numbers)
In the above example, we assign values to variables, perform arithmetic operations, and create a vector.
Data Structures in R
R supports various data structures such as vectors, matrices, lists, and data frames. Here's an example of a data frame:
# Creating a data frame
df <- data.frame(
Name = c("Alice", "Bob", "Charlie"),
Age = c(25, 30, 35),
Gender = c("Female", "Male", "Male")
)
# Displaying the data frame
print(df)
Data frames are commonly used for storing and manipulating tabular data.
Next: Variables and Data Types