R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
In R, a variable provides a way of naming and storing data for later use. They can store a variety of data types, including numbers, strings, vectors, and more. This tutorial will provide an overview of how to use variables in R.
In R, the <-
symbol is often used for assignment, though =
can also be used.
x <- 5 # Assigning the value 5 to x name <- "Alice" # Assigning the string "Alice" to name
You can assign multiple values to multiple variables at once.
x <- y <- z <- 10 # Assigning 10 to x, y, and z
Once a variable has been assigned, you can use it just like you would use the value itself.
x <- 5 y <- 10 sum <- x + y # Adding the values of x and y, and storing in sum print(sum) # Outputs: 15
Variables in R can hold different types of data. Here are some of the common ones:
Numbers, including integers and decimals.
num1 <- 5 num2 <- 5.5
Strings of text.
str1 <- "Hello" str2 <- 'World'
Boolean values: TRUE
or FALSE
.
bool1 <- TRUE bool2 <- (5 > 10) # This will evaluate to FALSE
Ordered collections of values. All values in a vector must be of the same type.
vec1 <- c(1, 2, 3, 4, 5) vec2 <- c("a", "b", "c")
There are many other advanced types like factors, lists, data frames, and matrices, which you'll encounter as you dive deeper into R.
There are some best practices when naming variables in R:
user_age
, first_name
.c
, mean
, etc.result
, Result
, and RESULT
are three different variables.You can see all the variables in your workspace using the ls()
function.
ls()
To remove a variable, use the rm()
function.
x <- 5 rm(x) # This will remove the variable x
Variables are the building blocks of any programming language, including R. Properly naming, assigning, and managing your variables is crucial for writing clear and efficient R scripts. As you get more advanced, you'll also encounter more complex data structures and types, expanding the ways you can store and manipulate data.
Creating and assigning variables in R:
<-
or =
.# Example of creating and assigning variables age <- 25 name <- "John"
Data types of variables in R programming:
# Example of data types in R variables numeric_var <- 42 character_var <- "Hello, R!" logical_var <- TRUE
Variable naming conventions in R:
# Example of variable naming conventions user_name <- "Alice"
Scope and visibility of variables in R:
# Example of global and local variables global_var <- 10 my_function <- function() { local_var <- 5 }
Global and local variables in R:
# Example of global and local variables global_var <- 10 my_function <- function() { local_var <- 5 }
Checking and changing variable types in R:
class()
to check variable types, and as.numeric()
to change types.# Example of checking and changing variable types age <- "25" class_age <- class(age) numeric_age <- as.numeric(age)
Missing values and variables in R:
NA
and handle them using functions like is.na()
.# Example of missing values in variables height <- c(160, NA, 175, 168) missing_values <- is.na(height)
R code examples for variable manipulation:
# Example of variable manipulation x <- 5 y <- 10 sum_result <- x + y