Learn R Language Essentials concepts: creating variables, user input methods, handling impossible values (NA/NaN), memory limits, and binary operators. Perfect for R beginners and data science learners.
Table of Contents
R Language Essentials: Variables, Input, Memory & Operators
How to create a new variable in R?
In the R language, there are many ways to create a new variable, depending on your data structure and needs. Here are some important ways to create a new variable in R.
Assignment Operator
For creating a new variable assignment operator, ‘<-‘ is used. For example,
mydata$sum <- mydata$x1 + mydata$x2
Using the $ operator (for data frames)
# Create a data frame df <- data.frame(x = 1:5, y = 6:10) # Add a new variable df$z <- df$x + df$y ## Output df$z [1] 7 9 11 13 15
Using bracket notation [ ]
df["new_var"] <- df$x * 2
Using the transform() function
df <- transform(df,
sum_xy = x + y,
product_xy = x * y)Using within() function
df <- within(df, {
ratio <- x / y
squared_diff <- (x - y)^2
})Creating variables in vectors
# Create a vector
my_vector <- c(1, 2, 3, 4, 5)
# Add names to vector elements
names(my_vector) <- c("a", "b", "c", "d", "e")
# Create new vector from existing
new_vector <- my_vector * 2How to request input from the user through keyboard and monitor?
In the R language, there is a series of functions that can be used to request input from the user, including readline(), cat(), and scan(). But I find the readline() function to be the optimal function for this task.
readline() – Basic Text Input
# Simple text input
name <- readline(prompt = "Enter your name: ")
age <- as.numeric(readline(prompt = "Enter your age: "))
cat("Hello", name, "! You are", age, "years old.\n")Basic Keyboard Input
# Read numeric input from keyboard
cat("Enter numbers (press Enter twice to finish):\n")
numbers <- scan()
# Read character input
cat("Enter text (press Enter twice to finish):\n")
text <- scan(what = character())
# Read with prompt for each input
values <- scan(n = 5) # Reads exactly 5 valuesHow are impossible values represented in R?
In the R language, impossible or undefined values are represented using special values and NA types.
NaN (Not a Number)
Represents mathematically undefined numeric operations.
# Operations that produce NaN 0 / 0 # NaN - 0 divided by 0 Inf - Inf # NaN - Infinity minus infinity Inf / Inf # NaN - Infinity divided by infinity sqrt(-1) # NaN - Square root of negative number log(-1) # NaN - Log of negative number asin(2) # NaN - Arcsin of number > 1 # Check for NaN is.nan(0/0) # TRUE is.nan(5) # FALSE
Inf and -Inf (Infinity)
Represent positive and negative infinity.
# Positive infinity 1 / 0 # Inf exp(1000) # Inf (if result exceeds limits) 10^1000 # Inf # Negative infinity -1 / 0 # -Inf log(0) # -Inf # Check for infinity is.infinite(1/0) # TRUE is.finite(1/0) # FALSE
NA (Not Available)
Represents missing or undefined values.
# Different NA types numeric_na <- NA_real_ # Numeric NA integer_na <- NA_integer_ # Integer NA character_na <- NA_character_ # Character NA logical_na <- NA # Logical NA (default) # Check for NA is.na(NA) # TRUE is.na(5) # FALSE
NULL
Represents an empty or undefined object (different from NA).
# NULL examples
empty_list <- NULL
uninitialized_var <- NULL
# Functions returning NULL
result <- print("hello") # print() returns NULL
# Check for NULL
is.null(NULL) # TRUE
is.null(NA) # FALSE (NA is not NULL!)What is the memory limit of R?
The memory limit in the R language depends on several factors, including your operating system, R version, architecture (32-bit vs 64-bit), and system configuration.
Operating System Differences
For Windows Operating Systems
# Check memory limit on Windows memory.limit() # Returns current limit in MB memory.size() # Current memory usage in MB memory.size(max = TRUE) # Maximum memory used # Set memory limit (Windows only) memory.limit(size = 16000) # Set to 16GB
For MacOS and Linux Systems
# No explicit memory limit functions
# Limited by system RAM and swap space
# Check system memory
system("free -h", intern = TRUE) # Linux
system("vm_stat", intern = TRUE) # macOS32-bit vs 64-bit Architecture
32-bit R
- Maximum addressable memory: ~4GB
- Practical limit: ~3-3.5GB
- Vector size limit: 2^31-1 elements (~2.1 billion)
- Common issue: “Cannot allocate vector of size…”
64-bit R
- Theoretical limit: 8TB on 64-bit Windows, much larger on Linux/macOS
- Vector size limit: 2^48-1 elements on Windows, 2^64-1 on Linux/macOS
- Practical limit: Your available RAM + swap space
# Check if you're running 64-bit R .Platform$r_arch # "x64" for 64-bit, "" for 32-bit .Machine$sizeof.pointer # 8 for 64-bit, 4 for 32-bit # Maximum vector length .Machine$integer.max # 2147483647 (2^31-1)
On which type of data do binary operators in R work?
Binary operators in the R language work on various data types, but their behavior depends on the types of operands involved. Binary operators are applied to matrices, vectors, and scalars.



