Variables in R are used to store data values such as numbers, strings, or other types. You can think of a variable as a container that holds information which you can use and manipulate in your program.
You assign a value to a variable using the <-
operator or the equal sign =
. The assignment operator <-
is more common in R.
# Assigning value 10 to variable x x <- 10 # Assigning a text string to variable name name <- "R Language" # Using equal sign (also valid) y = 20
.
), and underscores (_
).myVar
and myvar
are different).if
, for
, TRUE
, etc.) as variable names.To see the value stored in a variable, simply type the variable name:
x # Output: [1] 10 name # Output: [1] "R Language"
R automatically assigns the data type based on the value assigned. Common types are:
10
, 3.14
)"Hello"
)TRUE
, FALSE
)L
suffix (5L
)class(x) # numeric class(name) # character class(TRUE) # logical class(5L) # integer
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!