Introduction to syntax in R

Overview

R is a programming language used for statistical analysis, graphical presentation, and reporting. It also helps with data cleaning, analysis, and visualization.

Number output in R

In R, we type the number without a quote to return a number as an output.

Example

# to print 5
5
# to print 2.5
2.5
# to print -20
-20
# to print some other numbers
10
-1.5

Text output in R

In R, we use single or double quotes to return a text as an output.

Example

'Hello World'
"Hello World"

Calculations in R

To perform calculations in R, we type the numbers together with the mathematical operator sign.

Example

# Addition
10 + 5
# Subtraction
10 - 5
# Multiplication
10 * 5
# Division
10 / 5
# Modulo
10 %% 5
# Exponentiation
10 ^ 5

Print in R

R does not require the print function to return an output of a code. It has the print() function if we wish to use it. We use the print() function only when we’re working with for loops.

Example

"Hello world"
'Hello World'
print('Hello World')
# using a for loop
for (x in 1:5) {
print(x)
}

Note: Using the print() function is only compulsory with for loop. This is because the code is inside curly braces {}.

Free Resources