R is a programming language and free software environment for statistical computing and graphics supported by the R Foundation for Statistical Computing. It is widely used among statisticians and data miners for developing statistical software and data analysis.
Key features of R include:
- Effective data handling and storage facility
- A suite of operators for calculations on arrays, in particular matrices
- A large, coherent, integrated collection of intermediate tools for data analysis
- Graphical facilities for data analysis and display
- A well-developed, simple and effective programming language
R was created by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, and is now developed by the R Development Core Team.
print("Hello, World!")
R is an interpreted language which means that commands are executed directly without needing to compile a program.
Basic R syntax includes:
- Assignment operators: <- or =
- Comments start with #
- Function calls use parentheses
Here are some basic examples:
x <- 5 # Assignment
y = 10 # Alternative assignment
x + y # Addition
R is case-sensitive, so 'Variable' and 'variable' would be two different objects.
# This is a comment
my_var <- 42
print(my_var)
R has several basic data types:
- Numeric: decimal numbers (default type for numbers)
- Integer: whole numbers
- Logical: TRUE or FALSE
- Character: strings
- Complex: complex numbers
You can check the type of a variable using the class() function:
x <- 10.5
class(x) # "numeric"
y <- 10L
class(y) # "integer"
z <- TRUE
class(z) # "logical"
R also has special values:
NA # Not Available/Not Applicable
NULL # Null object
Inf # Infinity
NaN # Not a Number
Control structures allow you to control the flow of execution of the program. R provides standard control structures similar to other languages:
Conditional statements:
# if statement
x <- 5
if (x > 0) {
print("Positive number")
} else if (x < 0) {
print("Negative number")
} else {
print("Zero")
}
Loops:
# for loop
for (i in 1:5) {
print(i)
}
# while loop
count <- 1
while (count <= 5) {
print(count)
count <- count + 1
}
R also has vectorized operations that often eliminate the need for explicit loops:
x <- 1:5
y <- x * 2 # Vectorized operation
Functions are fundamental building blocks in R. You can create your own functions using the function() function:
# Function definition
square <- function(x) {
return(x * x)
}
# Function call
square(5) # Returns 25
Functions can have default arguments:
power <- function(x, exponent = 2) {
return(x ^ exponent)
}
power(3) # Returns 9 (3^2)
power(3, 3) # Returns 27 (3^3)
R has many built-in functions for statistical operations:
mean(c(1, 2, 3, 4, 5)) # Returns 3
sd(c(1, 2, 3, 4, 5)) # Standard deviation
summary(c(1, 2, 3, 4, 5)) # Statistical summary
Anonymous functions (lambdas) are also supported:
sapply(1:5, function(x) x^2) # Returns c(1, 4, 9, 16, 25)
Data frames are the most important data structure in R for data analysis. They are similar to tables in a database or Excel spreadsheets.
Creating a data frame:
# Create a data frame
df <- data.frame(
name = c("Alice", "Bob", "Charlie"),
age = c(25, 30, 35),
score = c(88.5, 92.0, 85.5)
)
# View the data frame
print(df)
Accessing data frame elements:
df$name # Access column
df[1, ] # First row
df[, 2] # Second column
df[2, "age"] # Specific cell
Common operations on data frames:
head(df) # View first few rows
summary(df) # Summary statistics
dim(df) # Dimensions (rows, columns)
names(df) # Column names
R's dplyr package provides a powerful grammar for data manipulation:
library(dplyr)
df %>% filter(age > 25) %>% arrange(desc(score))