top of page

R Variables and Constants

In computer programming, a variable is a named memory location where data is stored. For example,

x = 13.8

Here, x is the variable where the data 13.8 is stored. Now, whenever we use x in our program, we will get 13.8.


x = 13.8  
# print variable
print(x)

Output

[1] 13.8

As you can see, when we print x we get 13.8 as output.


Rules to Declare R Variables

As per our requirements, we can use any name for our variables. However, there are certain rules that need to be followed while creating a variable:

  • A variable name in R can be created using letters, digits, periods, and underscores.

  • You can start a variable name with a letter or a period, but not with digits.

  • If a variable name starts with a dot, you can't follow it with digits.

  • R is case sensitive. This means that age and Age are treated as different variables.

  • We have some reserved words that cannot be used as variable names.

Note: In earlier versions of R programming, the period . was used to join words in a multi-word variable such as first.name, my.age, etc. However, nowadays we mostly use _ for multi-word variables For example, first_name, my_age, etc.


Types of R Variables

Depending on the type of data that you want to store, variables can be divided into the following types.

1. Boolean Variables

It stores single bit data which is either TRUE or FALSE. Here, TRUE means yes and FALSE means no. For example,

a = TRUE  
print(a) 
print(class(a))

Output

[1] TRUE
[1] "logical"

Here, we have declared the boolean variable a with the value TRUE. Boolean variables belong to the logical class so class(a) returns "logical".


2. Integer Variables

It stores numeric data without any decimal values. For example,


A = 14L  
print(A) 
print(class(A))

Output

[1] 14
[1] "integer"

Here, L represents integer value. In R, integer variables belong to the integer class so, class(a) returns "integer".


3. Floating Point Variables

It stores numeric data with decimal values. For example,

x = 13.4print(x) 
print(class(x))

Output

[1] 13.4
[1] "numeric"

Here, we have created a floating point variable named x. You can see that the floating point variable belongs to the numeric class.


4. Character Variables

It stores a single character data. For example,

alphabet = "a"
print(alphabet) 
print(class(alphabet))

Output

[1] "a"
[1] "character"

Here, we have created a character variable named alphabet. Since character variables belong to the character class, class(alphabet) returns "character".


5. String Variables

It stores data that is composed of more than one character. We use double quotes to represent string data. For example,

message = "Welcome to codeswithpankaj"
print(message) 
print(class(message))

Output

[1] "Welcome to codeswithpankaj"
[1] "character"

Here, we have created a string variable named message. You can see that the string variable also belongs to the character class


Changing Value of Variables

Depending on the conditions or information passed into the program, you can change the value of a variable. For example,


message = "Hello World!"
print(message)  
# changing value of a variable 
message <- "Welcome to codeswithpankaj"
print(message)

Output

[1] "Hello World!"
[1] "Welcome to codeswithpankaj"

In this program,

  • "Hello World!" - initial value of message

  • "Welcome to codeswithpankaj" - changed value of message

You can see that the value of a variable can be changed anytime.


R Constants

Constants are those entities whose values aren't meant to be changed anywhere throughout the code. In R, we can declare constants using the <- symbol. For example,


x <- "Welcome to codeswithpankaj"
print(x)

Output

[1] "Welcome to codeswithpankaj"

Here, "Welcome to codeswithpankaj" is a string constant.


Note: Constants are also known as scalars.


Types of R Constants

In R, we have the following types of constants.

  • The five types of R constants - numeric, integer, complex, logical, string.

  • In addition to these, there are 4 specific types of R constants - Null, NA, Inf, NaN.

Let's discuss each of these types one by one.

1. Integer Constants

Integer constants are the integer values we use in our code. These constants end with the letter L. For example,


x <- 15L print(typeof(x)) 
print(class(x))

Output

[1] "integer"
[1] "integer"

Here, 15L is a constant which has been assigned to x. You can see that the type and class of the constant is integer.


We can use different types of integer constants in our code. For example,


# hexadecimal valuex <- 0x15L
print(x)  # exponential valuex <- 1e5L
print(x)

Output


[1] 21
[1] 100000

2. Numeric Constants

In R programming, numeric constants can be integers (4), floating-point numbers (0.55), or exponential numbers (3e-3). For example,

z <- 3e-3
print(z)  
# 0.003print(class(z))  
# "numeric"y <- 3.4
print(y)  
# 3.4print(class(z))  
# "numeric"

Output

[1] 0.003
[1] "numeric"
[1] 3.4
[1] "numeric"

4. String Constants


String constants are the string data we use in our code. For example,


message <- "Welcome to codeswithpankaj"
print(message)

Output


[1] "Welcome to codeswithpankaj"

5. Complex Constants


A complex constant is data that contains a real and an imaginary part (denoted by the suffix i). For example,


y <- 3.2e-1i 
print(y) 
print(typeof(y))

Output

[1] 0+0.32i
[1] "complex"

Note: Complex constants can only be purely imaginary. For example,

y <- 3i print(y)    # 0+3iprint(typeof(y))  # "complex"


Special R Constants

R programming also provides 4 special types of constants.


NULL - to declare an empty R object. For example,

x <- NULL
print(x)  # NULL
print(typeof(x))  # "NULL"

Inf/-Inf - represents positive and negative infinity. For example,

# result is too big so it represents positive infinity
a <- 2^2020
print(a)   # Inf

# result is too big
# represents negative infinity
b <- -2^2020
print(b)    # -Inf

NaN (Not a Number) - represents undefined numerical value. For example,

print(0/0)      # NaN
print(Inf/Inf)  # NaN

NA - represents value which is not available. For example,

print(NA + 20) # NA

Built-In R Constants

R programming provides some predefined constants that can be directly used in our program. For example,


# print list of uppercase letters
print(LETTERS)

# print list of lowercase letters
print(letters)

# print 3 letters abbreviation of English months
print(month.abb)

# print numerical value of constant pi
print(pi)

Output

 [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
[20] "T" "U" "V" "W" "X" "Y" "Z"
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u" "v" "w" "x" "y" "z"
 [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
[1] 3.141593

In the above example, we have used the following built-in constants:

  • LETTERS - to display a list of all uppercase letters

  • letters - to display a list of all small letters

  • month.abb - to print 3 letter abbreviations of all English months

  • pi - to print the numerical value of the constant pi

Related Posts

See All

A Comprehensive Guide to R Data Types

Introduction In the realm of data analysis and statistical computing, the R programming language stands as a powerful tool. Understanding data types in R is fundamental to unleashing its full potentia

R - CSV Files

Getting and Setting the Working Directory You can check which directory the R workspace is pointing to using the getwd() function. You can also set a new working directory using setwd()function. # Get

Comments


bottom of page