📚 R Programming Tutorial

R Programming

Learn R programming concepts with simple explanations, examples, techniques and practical notes.

← Back to R Programming

Variables in R

A variable is a named memory location where we can keep values for a specific program. In simpler terms, a variable is a name that points to a memory location.

A variable is also called an identifier and is used to store a value.

Creating Variables in R

In R, do not need to declare a variable explicitly. When a value is assigned to a variable, it is automatically declared.

To assign a value to a variable, use the <- symbol. To print the variable value, just type the variable name.

Syntax:

variable_name <- value

Example

Filename: variables.R

# creating variables

sname <- "Naveen"

sage <- 20

# printing variables

sname

sage

Output:

Rscript variables.R

[1] "Naveen"

[1] 20

In the above example, sname and sage are variables, while "Naveen" and 20 are values.

In R, unlike other programming languages, you don't need to use a function to display or print variables. You can simply write the variable's name.

However, R does have a print() function to print/output variables. This might be useful if you are familiar with other programming languages, such as Python.

Example

Filename: variables_p.R

# creating variables

sname <- "Naveen"

sage <- 20

# printing variables

print (sname)

print (sage)

Output:

Rscript variables_p.R

[1] "Naveen"

[1] 20

Remainders

In many programming languages, the = sign is typically used for assigning values. But in R, you can use both = and <- for assignments. Most of the time, though, it's better to use <- since there are some cases in R where using = might not be allowed.

You can choose whether or not to use the print() function to display output of your code. But if your code is within an R expression (like inside curly braces {}), you should use the print() function to display the result.

Rules for Naming Variables in R

In R, a variable can be given a simple name, such as a or b, or it can have a more detailed name like first_name, age, or fullname.

There are some rules for naming variables in R:

  • A variable name must begin with a letter or a period (.) and can be followed by letters, numbers, a period (.), or an underscore (_).
  • If the variable starts with a period (.), it cannot be followed by a digit.
  • A variable name cannot start with an underscore (_) or a number.
  • Variable names are case-sensitive (i.e., variables a and A are different).
  • Reserved words are not allowed for use as variable names (if, TRUE, NULL, etc.).

Examples for Valid Variable Names

firstname <- "Naveen"

first_name <- "Naveen"

firstName <- "Madhu"

FIRSTNAME <- "Madhu"

name1 <- "Durga"

.fname <- "Durga"

Examples for Invalid Variable Names

first name <- "Naveen"

first-name <- "Naveen"

first@Name <- "Madhu"

_FNAME <- "Madhu"

1name <- "Durga"

.1name <- "Durga"

Multiple Variables

R allows us to assign a single value to multiple variables in one line.

Syntax:

var1 <- var2 <- var3 <- value

Example

Filename: variables_m.R

# Assign one value to multiple variables in a single line

a <- b <- c <- 10

# Print variable values

print (a)

print (b)

print (c)

Output:

Rscript variables_m.R

[1] 10

[1] 10

[1] 10