R Variables

 

R Variables

Introduction

A variable is a named memory location used to store data values. The value stored in a variable may change during the execution of a program; hence it is called a variable. The variable name is used to access and manipulate the stored data.

In R, variables are dynamically typed, which means that variables do not need to be declared with a specific data type. Instead, they automatically acquire the data type of the value assigned to them.


Variable Assignment in R

R supports three methods for assigning values to variables.

1. Using the Equal Operator (=)

The equal sign assigns a value to a variable.

Syntax

variable_name = value

Example

var1 = "Hello"
print(var1)

Output

[1] "Hello"

2. Using the Leftward Assignment Operator (<-)

The leftward assignment operator copies data from right to left. This is the most commonly used assignment operator in R.

Syntax

variable_name <- value

Example

var2 <- "Hello"
print(var2)

Output

[1] "Hello"

3. Using the Rightward Assignment Operator (->)

The rightward assignment operator copies data from left to right.

Syntax

value -> variable_name

Example

"Hello" -> var3
print(var3)

Output

[1] "Hello"

Example of Variable Assignment

# Using equal operator
var1 = "Hello"

# Using leftward operator
var2 <- "Hello"

# Using rightward operator
"Hello" -> var3

print(var1)
print(var2)
print(var3)

Output

[1] "Hello"
[1] "Hello"
[1] "Hello"

Naming Rules for Variables

The following rules should be followed while naming variables in R:

  1. A variable name may contain letters, digits, dots (.), and underscores (_).

    Valid Examples

    var1
    var_1
    var.1
  2. Special characters other than dot (.) and underscore (_) are not allowed.

    Invalid Examples

    var$1
    var#1
  3. Variable names should start with a letter or a dot.

    Valid Examples

    age
    .name
  4. Variable names should not start with a number or underscore.

    Invalid Examples

    2var
    _var
  5. If a variable starts with a dot, the character following the dot should not be a digit.

    Invalid Example

    .3var

Important Functions Used with Variables

1. class() Function

The class() function is used to determine the data type (class) of a variable.

Syntax

class(variable)

Example

var1 <- "Hello"

print(class(var1))

Output

[1] "character"

2. ls() Function

The ls() function lists all variables currently present in the workspace.

Syntax

ls()

Example

var1 <- "Hello"
var2 <- 10
TRUE -> var3

print(ls())

Output

[1] "var1" "var2" "var3"

Uses

  • Displays all existing variables.
  • Helps avoid overwriting variables.
  • Useful when working with large programs.

3. rm() Function

The rm() function removes variables from the workspace and frees memory.

Syntax

rm(variable)

Example

var1 <- "Hello"
var2 <- 100

rm(var2)

print(ls())

Output

[1] "var1"

Attempting to access the deleted variable results in an error:

print(var2)

Output

Error in print(var2) :
object 'var2' not found

Multiple Variable Deletion

Multiple variables can be removed simultaneously.

Example

x <- 10
y <- 20
z <- 30

rm(x, y)

print(ls())

Output

[1] "z"

Removing All Variables

All variables in the workspace can be deleted using:

rm(list = ls())

Example

a <- 10
b <- 20

rm(list = ls())

print(ls())

Output

character(0)

Summary of Variable-Related Functions

FunctionPurpose
class()    Determines the data type of a variable
ls()    Lists all variables in the workspace
rm()    Removes specified variables
rm(list=ls())    Removes all variables from the workspace

Conclusion

Variables in R are dynamically typed and can be created using the assignment operators =, <-, and ->. Proper naming conventions should be followed while creating variables. Functions such as class(), ls(), and rm() help users determine the data type of variables, list existing variables, and manage memory by deleting unwanted variables. These features make variable management in R simple and efficient.

Comments

Popular posts from this blog

Statistical Methods Lab ( R Language) PCCBL308 Semester 3 KTU BTech CB and CU 2024 Scheme - Dr Binu V P

Programs in R - using control statements - Assignment 2

Basic R Programs to Try - Assignment 1