R is a powerful programming language and environment primarily used for statistical computing and graphics.
I’ll explain you the important basic functions and data types in R along with easy to understand examples.
Basic Data Types
1. Numeric (double)
This data type represents real numbers and is the default type for storing numerical values.
# Numeric data type example x <- 3.14 y <- 42.0
2. Integer
Integers are whole numbers without a fractional component.
# Integer data type example z <- 10L # 'L' suffix indicates an integer
3. Character (string)
Character data type is used to represent text.
# Character data type example name <- "John Doe"
4. Logical (Boolean)
Logical data type represents Boolean values `TRUE` or `FALSE`.
# Logical data type example is_valid <- TRUE
5. Complex
Complex data type consists of two components: real and imaginary parts.
# Logical data type example is_valid <- TRUE
Basic Functions
1. print()
Prints its argument.
# print() function example print("Hello, World!")
2. c()
Concatenates its arguments.
# c() function example numbers <- c(1, 2, 3, 4, 5)
3. seq()
Generates sequences of numbers.
# seq() function example sequence <- seq(1, 10, by = 2) # Generates sequence from 1 to 10 by 2
4. rep()
Replicates values.
# rep() function example repeated <- rep("hello", times = 3) # Repeats "hello" 3 times
5. length()
Returns the number of elements in a vector.
# length() function example vec <- c(1, 2, 3, 4, 5) length(vec) # Returns 5
6. sum()
Computes the sum of values.
# sum() function example sum_of_values <- sum(1, 2, 3, 4, 5) # Returns 15
7. mean()
Computes the mean of values.
# mean() function example average <- mean(1, 2, 3, 4, 5) # Returns 3
8. max() and min()
Return the maximum and minimum values respectively.
# max() and min() function examples max_value <- max(1, 2, 3, 4, 5) # Returns 5 min_value <- min(1, 2, 3, 4, 5) # Returns 1
These are just a few examples of basic functions and data types in R which are important to learn.
As we learn R further, I’ll will take you through a wide range of functions and data structures tailored to statistical analysis and data manipulation.