R Variables

Introduction

In this chapter, you will learn about variables in R, how to create them, and the rules for naming them. Understanding how to use variables effectively is crucial for storing and manipulating data in your R programs.

Creating Variables

Variables in R are used to store data values. You can create a variable by assigning a value to it using the assignment operator <-. You can also use the = operator, but <- is the preferred operator in R programming.

Example: Creating Variables

# Using the assignment operator
x <- 10

# Using the equal operator
y = 20

# Printing the values of variables
print(x)  # Output: 10
print(y)  # Output: 20

Variable Naming Rules

When naming variables in R, you must follow these rules:

  1. Variable names must start with a letter (a-z, A-Z) or a period (.) followed by a letter.
  2. Variable names can contain letters, numbers (0-9), periods (.), and underscores (_).
  3. Variable names are case-sensitive (e.g., myVar and myvar are different).
  4. Avoid using reserved words (e.g., if, else, for, etc.) as variable names.

Example: Valid and Invalid Variable Names

# Valid variable names
myVar <- 100
_my_var <- 200
my.var <- 300
myVar1 <- 400

# Invalid variable names
# 1myVar <- 500   # Starts with a number
# my-var <- 600   # Contains a hyphen
# if <- 700       # Reserved word

Checking Variable Type

You can check the type of a variable using the class() function.

Example: Checking Variable Type

# Creating variables of different types
num_var <- 10.5
int_var <- 10L
char_var <- "Hello, R!"
bool_var <- TRUE

# Checking the types of variables
print(class(num_var))  # Output: "numeric"
print(class(int_var))  # Output: "integer"
print(class(char_var))  # Output: "character"
print(class(bool_var))  # Output: "logical"

Reassigning Variables

You can reassign variables to new values, even of a different type.

Example: Reassigning Variables

# Initial assignment
x <- 10
print(x)  # Output: 10

# Reassigning to a new value
x <- "Hello, R!"
print(x)  # Output: "Hello, R!"

Deleting Variables

You can delete a variable using the rm() function. Once deleted, the variable is no longer available in the current R session.

Example: Deleting Variables

# Creating a variable
z <- 50
print(z)  # Output: 50

# Deleting the variable
rm(z)

# Trying to print the deleted variable
# print(z)  # Error: object 'z' not found

Example Program with Variables

Here is an example program that demonstrates the use of variables in R:

# R Program to Demonstrate Variables

# Creating variables
a <- 5
b <- 10
c <- a + b

# Printing variables
print(a)  # Output: 5
print(b)  # Output: 10
print(c)  # Output: 15

# Reassigning variables
a <- "R Programming"
print(a)  # Output: "R Programming"

# Checking variable types
print(class(a))  # Output: "character"
print(class(b))  # Output: "numeric"

# Deleting a variable
rm(b)

# Trying to print the deleted variable
# print(b)  # Error: object 'b' not found

Conclusion

In this chapter, you learned about variables in R, including how to create, name, reassign, and delete them. Understanding how to use variables effectively is crucial for storing and manipulating data in your R programs. By following the rules for naming variables and knowing how to check their types, you can write clear and efficient R code.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top