r basic syntax
r basic syntax

R Basic Syntax

Posted on

R Basic Syntax

Before we go, we assumes R is already installed on your computer. We will be using RStudio but we can also use R command prompt by typing the following command in the command line. This will launch the interpreter 

$ R

In order to output text in R, use single or double quotes:

"hello"

[1] “hello”

In order to output numbers, just type the number (without quotes):

1
2
3

> 1
[1] 1
> 2
[1] 2
> 3
[1] 3

To do simple calculations, just simply add numbers together:

1+2+3

> 1+2+3
[1] 6

R Print

In R you can output code without using a print function:

"Hello world"

Hello world

However, R it self has a print() function available if you willing to use it. This might be useful if you are familiar with other programming languages, such as Python, which often uses the print() to output code. And there are conditions you must use the print() function to output code, for example when working with for loops codes.

print("Hello World!")

> print(“Hello World!”)
[1] “Hello World!”

for (a in 1:5) {
  print(a)
}

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5