R Variables

R Variables

Variables act as highly secure storage containers for data values.

In R, a variable instantly stores a value the moment you assign it.


Creating a Variable

Unlike C or Java, R does not require you to declare a variable type before using it.

To assign a value to a variable, you typically use the leftward assignment operator (<-).

While the equals sign (=) also works, the <- operator is the strictly preferred standard in R.

Variable Example:

# Assigning text to a variable
name <- "Akash"
# Assigning a number to a variable
age <- 25

print(name) print(age)


Variable Naming Rules

To prevent compiler errors, variable names must follow strict syntactic rules.

A variable name must start with a letter, or a period followed by a letter.

Variable names are strictly case-sensitive (e.g., age and Age are two entirely different variables).

They can only contain letters, numbers, periods (.), and underscores (_).


Outputting Variables

You can output multiple variables by simply typing their names into the console.

If you want to combine text and a variable together, you can use the paste() function.

This efficiently links strings and variables seamlessly for display.


Exercise 1 of 2

?

Which operator is the widely accepted standard for assigning a value to a variable in R?

Exercise 2 of 2

?

Are variable names in R case-sensitive?