Basics of R Syntax
Introduction to R Syntax
R is a powerful language for statistical computing and data analysis. Understanding its basic syntax is essential for writing efficient and error-free code. This guide covers the fundamentals of R syntax, including case sensitivity, comments, and operators.
1. Case Sensitivity
R is case-sensitive. This means that variables, functions, and other identifiers must be used consistently in terms of uppercase and lowercase letters.
Example:
# Define two different variables
myVar <- 10
MyVar <- 20
# Print the variables
print(myVar) # Output: 10
print(MyVar) # Output: 20
In the example above, myVar and MyVar are treated as two different variables because R distinguishes between uppercase and lowercase letters.
2. Comments
Comments in R are used to explain code and make it more readable.
They are ignored by the R interpreter and do not affect the execution of the code.
Comments start with the # symbol.
Example:
# This is a single-line comment
x <- 5 # Assign the value 5 to variable x
# Multi-line comments can be written as follows:
# First line of comment
# Second line of comment
y <- 10
3. Operators
Operators in R are used to perform operations on variables and values. They are categorized into arithmetic, relational, logical, and assignment operators.
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations.
| Operator | Description | Example |
|---|---|---|
+ |
Addition | x + y |
- |
Subtraction | x - y |
* |
Multiplication | x * y |
/ |
Division | x / y |
%% |
Modulus (remainder) | x %% y |
^ |
Exponentiation | x ^ y |
Example:
a <- 10
b <- 3
# Addition
add_result <- a + b
print(add_result) # Output: 13
# Modulus
mod_result <- a %% b
print(mod_result) # Output: 1
Relational Operators
Relational operators are used to compare values.
| Operator | Description | Example |
|---|---|---|
== |
Equal to | x == y |
!= |
Not equal to | x != y |
> |
Greater than | x > y |
< |
Less than | x < y |
>= |
Greater than or equal to | x >= y |
<= |
Less than or equal to | x <= y |
Example:
x <- 10
y <- 20
# Equal to
equal_result <- x == y
print(equal_result) # Output: FALSE
# Greater than
greater_result <- x > y
print(greater_result) # Output: FALSE
Logical Operators
Logical operators are used to combine or negate boolean values.
| Operator | Description | Example |
|---|---|---|
& |
Logical AND | x > 5 & y > 15 |
| |
Logical OR | x > 5 | y < 15 |
! |
Logical NOT | !(x > 5) |
Example:
x <- TRUE
y <- FALSE
# Logical AND
and_result <- x & y
print(and_result) # Output: FALSE
# Logical NOT
not_result <- !x
print(not_result) # Output: FALSE
Assignment Operators
Assignment operators are used to assign values to variables.
| Operator | Description | Example |
|---|---|---|
<- |
Assign value | x <- 5 |
= |
Assign value | x = 5 |
Example:
# Using <- operator
x <- 10
# Using = operator
y = 20
Practical Exercise
Try the following exercises to practice R syntax:
- Create two variables,
lengthandwidth, and assign them values of 10 and 5, respectively. - Calculate the area of a rectangle using the formula
length * width. - Use comments to explain each step of your code.
- Check if the area is greater than 40 using a relational operator.
- Print the result of a logical operation that checks if the area is greater than 40 AND less than 60.
Solution:
# Define variables
length <- 10
width <- 5
# Calculate area
area <- length * width
# Check if area is greater than 40
is_greater <- area > 40
print(is_greater) # Output: FALSE
# Logical operation
logical_result <- area > 40 & area < 60
print(logical_result) # Output: TRUE
