Basic Syntax in R
In this lesson, you will learn how R code is written and executed. Understanding basic syntax is very important because every R program follows these rules.
Do not worry if this feels new. R syntax is simple, readable, and beginner-friendly.
What Is Syntax?
Syntax refers to the rules that define how a programming language is written. It tells the computer how to read and understand your instructions.
If syntax rules are broken, the program will show errors and stop running. That is why learning syntax early is very important.
Writing Your First R Command
In R, you can type commands directly into the console or write them in a script file. Let us start with a very simple command.
print("Welcome to R Programming")
This command tells R to display text on the screen. If you see the message printed, your command executed successfully.
Using the R Console
The R console is where commands are executed immediately. It is useful for quick tests and simple calculations.
Whenever you press Enter, R reads the command and shows the output below.
10 + 5
R calculates the result and prints the answer automatically.
Comments in R
Comments are lines that R ignores. They are used to explain code for humans, not for the computer.
In R, comments start with the # symbol. Anything written after # is ignored during execution.
# This is a comment
print("This line will run")
Using comments makes your code easier to understand and maintain.
R Is Case-Sensitive
R treats uppercase and lowercase letters as different. This means variable names and function names must be written exactly.
Even a small change in letter case can cause errors.
Print("Hello") # Error
print("Hello") # Correct
Always be careful with letter casing when writing R code.
Statements and Expressions
A statement is a complete instruction that R can execute. An expression is something that produces a value.
In R, most expressions automatically display their result.
5 * 4
100 / 10
R evaluates each expression and shows the result in the console.
Using Semicolons
In R, semicolons are optional. Most of the time, you do not need them.
However, semicolons can be used to write multiple commands on one line.
x <- 10; y <- 20; x + y
For beginners, it is recommended to write one command per line.
Assignment Operator in R
R commonly uses the <- symbol to assign values to variables. This arrow points from the value to the variable.
This style is widely used and preferred in R programming.
number <- 25
text <- "Dataplexa"
You will use this operator frequently in upcoming lessons.
Whitespace and Indentation
R ignores extra spaces and line breaks. However, clean formatting makes code easier to read.
Good indentation is especially important in loops and functions.
total <- 0
total <- total + 5
total <- total + 10
Always write code in a clean and organized way.
Common Beginner Syntax Errors
Here are some common syntax mistakes beginners make:
- Missing quotation marks
- Incorrect use of uppercase and lowercase letters
- Forgetting parentheses
- Using wrong symbols
When an error appears, read the message carefully. R usually tells you what went wrong.
What’s Next?
Now that you understand basic R syntax, you are ready to work with data.
In the next lesson, you will learn about variables in R and how to store values. This will help you write more meaningful programs.