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 55# to print 2.52.5# to print -20-20# to print some other numbers10-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
# Addition10 + 5# Subtraction10 - 5# Multiplication10 * 5# Division10 / 5# Modulo10 %% 5# Exponentiation10 ^ 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 loopfor (x in 1:5) {print(x)}
Note: Using the
print()function is only compulsory withforloop. This is because the code is inside curly braces{}.