Basic Syntax in R Programming

22 Apr, 2020
R is the most popular language used for Statistical Computing and Data Analysis with the support of over 10, 000+ free packages in CRAN repository.
Like any other programming language, R has a specific syntax which is important to understand if you want to make use of its powerful features.
This article assumes R is already installed on your machine. We will be using RStudio but we can also use R command prompt by typing the following command in the command line.
$ R
This will launch the interpreter and now let’s write a basic Hello World program to get started.
We can see that “Hello, World!” is being printed on the console.
Now we can do the same thing using print() which prints to the console. Usually, we will write our code inside scripts which are called RScripts in R. To create one, write the below given code in a file and save it as myFile.R and then run it in console by writing:
Rscript myFile.R
Output:
[1] "Hello, World!"
A program in R is made up of three things: Variables, Comments, and Keywords. Variables are used to store the data, Comments are used to improve code readability, and Keywords are reserved words that hold a specific meaning to the compiler.
Previously, we wrote all our code in a print() but we don’t have a way to address them as to perform further operations. This problem can be solved by using variables which like any other programming language are the name given to reserved memory locations that can store any type of data.
In R, the assignment can be denoted in three ways:
= (Simple Assignment)<- (Leftward Assignment)-> (Rightward Assignment)
= (Simple Assignment)
<- (Leftward Assignment)
-> (Rightward Assignment)
Example:
Output:
"Simple Assignment"
"Leftward Assignment!"
"Rightward Assignment"

Comments are a way to improve your code’s readability and are only meant for the user so the interpreter ignores it. Only single-line comments are available in R but we can also use multiline comments by using a simple trick which is shown below. Single line comments can be written by using # at the beginning of the statement.
Example:
Output:
[1] "This is fun!"
From the above output, we can see that both comments were ignored by the interpreter.
Keywords are the words reserved by a program because they have a special meaning thus a keyword can’t be used as a variable name, function name, etc.
We can view these keywords by using either help(reserved) or ?reserved.
if, else, repeat, while, function, for, in, next and break are used for control-flow statements and declaring user-defined functions.
The ones left are used as constants like TRUE/FALSE are used as boolean constants.
NaN defines Not a Number value and NULL are used to define an Undefined value.
Inf is used for Infinity values.
Note: R is a case sensitive language so TRUE is not same as True.
Picked
R-basics
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change column name of a given DataFrame in R
Filter data by multiple conditions in R using Dplyr
How to Replace specific values in column in R DataFrame ?
How to Split Column Into Multiple Columns in R DataFrame?
Loops in R (for, while, repeat)
Change Color of Bars in Barchart using ggplot2 in R
Adding elements in a vector in R programming - append() method
Group by function in R using Dplyr
How to change Row Names of DataFrame in R ?
Convert Factor to Numeric and Numeric to Factor in R Programming