R is a powerful programming language for statistical computing and data analysis. Understanding its syntax basics is crucial for effective coding. This guide will introduce you to the fundamental concepts of R syntax.
R code is typically written as a series of commands. Each command is usually written on a new line, but you can separate multiple commands on the same line using semicolons.
x <- 5
y <- 10
z <- x + y
In R, you can assign values to variables using the assignment operator <-
or =
. The <-
operator is more commonly used in R programming.
age <- 30
name = "John"
For more details on variables and assignments, check out the guide on R Variables and R Assignments.
R supports various data types, including:
42
, 3.14
"Hello"
, 'World'
TRUE
, FALSE
Learn more about specific data types in R:
Functions in R are called using parentheses. Arguments are passed inside these parentheses, separated by commas.
result <- sum(10, 20, 30)
print(result)
For a deeper dive into functions, visit the R Function Basics guide.
Vectors are one-dimensional arrays that can hold elements of the same data type. They are created using the c()
function.
numbers <- c(1, 2, 3, 4, 5)
fruits <- c("apple", "banana", "orange")
Explore more about vectors in the R Vectors guide.
R supports various operators for arithmetic, comparison, and logical operations:
Category | Examples |
---|---|
Arithmetic | + , - , * , / , ^ |
Comparison | == , != , < , > , <= , >= |
Logical | & , | , ! |
For more details, check out these guides:
Comments in R start with the #
symbol. Everything after #
on the same line is treated as a comment and ignored by the R interpreter.
# This is a comment
x <- 5 # This is an inline comment
Learn more about commenting your code in the R Comments guide.
These R syntax basics form the foundation for writing efficient R code. As you progress, you'll encounter more complex structures and functions. Remember to practice regularly and refer to the R documentation for detailed information on specific functions and features.
To further enhance your R programming skills, explore topics like R Data Frames, R If-Else Statements, and R For Loops.