đ Introduction
Variables are named storage locations used to hold data in an R program. They allow you to store values such as numbers, text, logical values, or complex data structures, and reuse them throughout your code. Variables make programs more flexible, readable, and easier to maintain.
Information
đ¯ Why Use Variables?
- Store data for later use.
- Avoid repeating the same values in multiple places.
- Improve code readability.
- Simplify calculations and data manipulation.
- Make programs easier to maintain and update.
đ Creating Variables
In R, variables are commonly created using the <- assignment operator. The value on the right-hand side is assigned to the variable on the left-hand side.
Creating Variables
name <- "Alice"
age <- 22
salary <- 55000
print(name)
print(age)
print(salary)Output
Console Output
[1] "Alice"
[1] 22
[1] 55000Tip
đ Assignment Operators
The <- operator is the standard and preferred assignment operator in R.
Using <-
x <- 100
print(x)The = operator can also assign values, although it is commonly used for function arguments.
Using =
y = 200
print(y)The -> operator assigns a value from left to right.
Using ->
300 -> z
print(z)đ Variable Naming Rules
| Rule | Example |
|---|---|
| Must begin with a letter or a period not followed by a number. | student, .value |
| Can contain letters, digits, underscores, and periods. | student_name |
| Cannot begin with a number. | 1student â |
| Variable names are case-sensitive. | Age â age |
| Reserved keywords cannot be used. | if, for â |
đ¤ Variable Naming Examples
Valid Variable Names
studentName <- "John"
student_name <- "John"
marks1 <- 90
.total <- 100Invalid Variable Names
1student <- "John"
student-name <- "John"
for <- 10đĻ Storing Different Data Types
Variables in R can store different kinds of data without requiring explicit type declarations.
Different Data Types
name <- "Emma"
age <- 24
height <- 5.6
isStudent <- TRUE
print(name)
print(age)
print(height)
print(isStudent)đ Updating Variable Values
The value stored in a variable can be modified at any time by assigning a new value.
Updating Variables
score <- 80
print(score)
score <- 95
print(score)Output
Console Output
[1] 80
[1] 95â Using Variables in Expressions
Variables can participate in arithmetic and other expressions to produce new values.
Arithmetic with Variables
a <- 15
b <- 10
sum <- a + b
product <- a * b
print(sum)
print(product)Output
Console Output
[1] 25
[1] 150đ§ Variable Lifecycle
đ Real-World Example
Student Information
student_name <- "Sophia"
course <- "Data Science"
marks <- 92
print(student_name)
print(course)
print(marks)â ī¸ Common Mistakes
| Mistake | Explanation | Solution |
|---|---|---|
| Using spaces in variable names | Variable names cannot contain spaces. | Use underscores or camelCase. |
| Beginning with a number | Variable names cannot start with digits. | Start with a letter or valid period. |
| Incorrect capitalization | R is case-sensitive. | Use consistent naming. |
| Using reserved keywords | Keywords have predefined meanings. | Choose meaningful alternative names. |
đĄ Best Practices
- Choose meaningful and descriptive variable names.
- Follow a consistent naming convention throughout your program.
- Use <- for assignment.
- Avoid single-letter variable names except for simple examples.
- Keep variable names concise but informative.
Best Practice
đ Summary
Variables are one of the fundamental building blocks of R programming. They allow you to store, modify, and reuse data efficiently throughout a program. Understanding how to create variables, follow naming rules, assign values, and use variables in expressions is essential before learning more advanced topics such as data structures, functions, loops, and data analysis.