Browse By

R Language – R Basic Syntax & Comments: Online Course

R Language – R Basic Syntax & Comment: Online Course:So many programming language are available but R language is used for specific Statistical Computing and Data Analysis. Like any other programming language, R also has a specific syntax that is important to understand and if you want to use its powerful features.

R Basic Syntax

R Command Prompt: Once you have R environment setup installs in your system to work on the R command prompt, then it’s easy to start your ‘command prompt by just typing the following command at your window command prompt

$ R

This will launch R interpreter and you will get a prompt > where you can start typing your program as follows –

You can run the program in two ways:

  1. Start coding on the terminal.
  2. Using an R Script file.

Start coding on the terminal.

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

Input: Info Khajana Program
myString <- “Info Khajana!”
print ( myString)
Output:
[1] “Info Khajana!”

Here first statement defines a string variable myString, where we assign a string “Hello, World!” and then next statement print() is being used to print the value stored in variable myString.

Start coding using an R Script file

Step 1:- Using a text editor program and Create a file called  “helloworld.R” . The .R file extension is used to specify Rscript file.

Step 2:- Let’s open the “helloworld.R” file that we created, and put the following line of code in it and save it.

Input: helloworld.R
> helloStr <- “Hello world!”
> print(helloStr)

Type following command to run the program.

Rscript helloworld.R

Once the program executed, it will show the output.

Output:
[1] “Hello world!”

R 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.

Comments are generally used for the following purposes:

  • Code is easily Reade able
  • Explanation of the code or Metadata of the project
  • Prevent execution of code
  • To include resources

Type of Comments

  • Single line Comments
  • Multi-line Comments

Single line Comments

Single comment is written using # in the beginning of the statement as follows −

Input: Single-line comment
# My First program in R programming
string <-“Info Khajana!”
print(string)
Output:
“Info Khajana!”

Multi-line Comments

R doesn’t support multi-line comment. However, we can just insert a # for each line to create multiline comments:

Input:Multi-line comment
# This is a comment
# written in
# more than just one line
# My First program in R programming

string <-“Info Khajana!”
print(string)
Output:
“Info Khajana!”

CONCLUSION

We have discussed in this article such as Basic Syntax of R and R comments . A good understanding of any other programming languages ​​will help you understand R programming concepts quickly.

Read Next Article in the Series-

Basic R Synatx

आशा करता हूँ, कि यह आर्टिकल आपको पसंद आया होगा तो सोच क्या रहे हैं अभी इसी वक्त इसे अपने दोस्तों के साथ सोशल मीडिया पर Share करें।

ThankingYou………………धन्यवाद………………..शुक्रिया………………..मेहरबानी…………………..

Reference

R Language

Read More

1. Introduction of R
2. Install R