Basic R Syntax
Basic R Syntax
R programming language has a simple and intuitive syntax, making it easy to learn for beginners. The syntax of a programming language refers to the set of rules that define the correct structure and format of code that the language can interpret and execute.
In R, variables are assigned using <-, =, or ->. The most common and recommended operator is <-. Note that R is a case-sensitive language.
R Command Prompt
You can enter the R code for a program at the R command prompt.
Launch the R GUI tool or RStudio IDE tool. Type the following commands at the R Console prompt.
> message <- “Hello, World!”
> print (message)
Output
[1] “Hello, R World!”
The first statement assigns a string value, “Hello, World!“, to the string variable, message. The print() function prints the value stored in the variable.
R script file
You can type all the code statements in a file, with .R file extension. We can execute these scripts at the command prompt with an R interpreter.
Example
A sample code that can be saved in a file called script.R:
The source(“filename”) function submits a script file to the current session of R. You can then run your Rscript, which contains a set of R statements.
> source(“C:/script.R”)
When you run the above program using the source() function, the following output is produced:
[1] “Hello, World!”
R Official website
For more information on the R language, visit the official website:
- https://www.r-project.org/