Saturday, November 21, 2020

Syntax of R Programming

 R Programming is a very popular programming language which is broadly used in data analysis. The way in which we define its code is quite simple. The "Hello World!" is the basic program for all the languages, and now we will understand the syntax of R programming with "Hello world" program. We can write our code either in command prompt, or we can use an R script file.

R Command Prompt

It is required that we have already installed the R environment set up in our system to work on the R command prompt. After the installation of R environment setup, we can easily start R command prompt by typing R in our Windows command prompt. When we press enter after typing R, it will launch interpreter, and we will get a prompt on which we can code our program.

Syntax of R Programming

"Hello, World!" Program

The code of "Hello World!" in R programming can be written as:

Syntax of R Programming

In the above code, the first statement defines a string variable string, where we assign a string "Hello World!". The next statement print() is used to print the value which is stored in the variable string.

R Script File

The R script file is another way on which we can write our programs, and then we execute those scripts at our command prompt with the help of R interpreter known as Rscript. We make a text file and write the following code. We will save this file with .R extension as:

Demo.R

  1. string <-"Hello World!"  
  2. print(string)  

To execute this file in Windows and other operating systems, the process will remain the same as mentioned below.

Syntax of R Programming

When we press enter it will give us the following output:

Syntax of R Programming

Comments

In R programming, comments are the programmer readable explanation in the source code of an R program. The purpose of adding these comments is to make the source code easier to understand. These comments are generally ignored by compilers and interpreters.

In R programming there is only single-line comment. R doesn't support multi-line comment. But if we want to perform multi-line comments, then we can add our code in a false block.

Single-line comment

  1. #My First program in R programming  
  2. string <-"Hello World!"  
  3. print(string)  

The trick for multi-line comment

  1. #Trick for multi-line comment  
  2. if(FALSE) {  
  3.    "R is an interpreted computer programming language which was created by  
  4.    Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand "  
  5. }  
  6. #My First program in R programming  
  7. string <-"Hello World!"  
  8. print(string)  


Syntax of R Programming

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...