# R

R is a programming language and free software environment for statistical computing and graphics. It is widely used for data analysis and statistical modeling, and is an implementation of the S programming language. R was developed in the early 1990s by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, and is now maintained by the R Development Core Team.

R is a powerful and flexible language that is widely used in academic and commercial settings for statistical analysis, data visualization, and predictive modeling. It is particularly popular in the fields of statistics, data science, and machine learning, and is widely used by researchers and data analysts to perform statistical analyses, create data visualizations, and build predictive models.

R has a large and active user community, with a wealth of online resources and tools available for learning and using the language. It is also supported by a wide range of third-party packages and libraries that can be used to extend its capabilities and perform a wide range of tasks.

*What can R do?*

R is used in a variety of fields, including statistics, data science, machine learning, and data visualization. Some of the best uses for R include:

- Statistical analysis: R is a popular choice for statistical analysis, with a wide range of built-in functions and packages for performing statistical tests and modeling data. It is particularly useful for analyzing large datasets and performing complex statistical analyses.
- Data visualization: R has a number of powerful packages and libraries for creating high-quality data visualizations, including plots, charts, and maps. It is widely used for creating informative and visually appealing visualizations of data.
- Machine learning: R has a number of packages and libraries for performing machine learning tasks, including classification, regression, and clustering. It is widely used for building predictive models and analyzing data for patterns and trends.
- Data manipulation and cleaning: R has a number of functions and packages for cleaning, transforming, and manipulating data. It is particularly useful for working with large and complex datasets, and is often used as part of the data preparation process.
- Data analysis: R is a powerful tool for analyzing data and uncovering insights. It is widely used in fields such as finance, marketing, and healthcare to analyze data and make informed decisions.

*Example code*

- Basic arithmetic operations:

```
# Add two numbers
x <- 2 + 3
# Subtract two numbers
y <- 5 - 2
# Multiply two numbers
z <- 3 * 4
# Divide two numbers
w <- 6 / 2
# Calculate the square root of a number
sqrt(25)
```

- Assigning values to variables:

```
# Assign a value to a variable
x <- 10
# Assign a character string to a variable
y <- "Hello, world!"
# Assign a vector to a variable
z <- c(1, 2, 3, 4, 5)
```

- Creating and accessing elements of a vector:

```
# Create a vector of numbers
x <- c(1, 2, 3, 4, 5)
# Access the third element of the vector
x[3]
# Access the second and fourth elements of the vector
x[c(2, 4)]
```

- Working with data frames:

```
# Load the built-in "mtcars" dataset
data("mtcars")
# View the first six rows of the dataset
head(mtcars)
# Select the "mpg" and "cyl" columns of the dataset
subset <- mtcars[, c("mpg", "cyl")]
# Calculate the mean of the "mpg" column
mean(mtcars$mpg)
# Create a scatterplot of "mpg" versus "cyl"
plot(mtcars$mpg, mtcars$cyl, xlab="Miles per gallon", ylab="Number of cylinders")
```