R Multiple Variables
🔢 R Multiple Variables
In R, you can create, assign, and work with multiple variables easily.
This is very important for data analysis, calculations, functions, and programming logic.
1️⃣ What are Variables in R?
A variable is a name that stores a value in memory.
Here:
-
xandyare variables -
<-is the assignment operator
2️⃣ Declaring Multiple Variables (Basic Way)
❌ Not compact
3️⃣ Assigning Multiple Variables in One Line ⭐
Using = and ;
Using <-
✔ Common in scripts
4️⃣ Assign Same Value to Multiple Variables ⭐
✔ All variables get value 100
5️⃣ Assign Multiple Values Using c() ⭐
6️⃣ Multiple Assignment Using list() ⭐⭐
This is the most powerful and interview-relevant method.
✔ Clean and professional
Example
7️⃣ Multiple Variables from Function Output ⭐
✔ Very useful in data analysis
8️⃣ Swap Values of Variables ⭐
📌 Logical approach (no temp variable)
9️⃣ Multiple Variables in a Single Expression
🔟 Checking Multiple Variables
✔ Shows all variables in environment
1️⃣1️⃣ Common Mistakes ❌
❌ Confusing multiple variables with vectors
❌ Forgetting order in list() assignment
❌ Using = and <- inconsistently
❌ Expecting c() to create multiple variables
📌 Interview Questions & MCQs
Q1. How to assign same value to multiple variables?
A) a, b <- 10
B) a <- b <- 10
C) a = b = 10
D) Both B and C
✅ Answer: D
Q2. Which method assigns multiple variables at once?
A) c()
B) vector()
C) list()
D) matrix()
✅ Answer: C
Q3. What does this do?
A) Creates 3 variables
B) Creates a vector
C) Error
D) Creates list
✅ Answer: B
Q4. Which operator is preferred in R?
A) =
B) <-
✅ Answer: B
Q5. Can R assign multiple variables from function output?
A) Yes
B) No
✅ Answer: A
🔥 Real-Life Use Cases
✔ Storing multiple results
✔ Data preprocessing
✔ Function outputs
✔ Statistical summaries
✔ Clean scripting
✅ Summary
-
R supports multiple variable assignments
-
Use
<-for best practice -
list()allows true multiple assignment -
c()creates vectors, not variables -
Essential for R programming & interviews
