comment in R: How to use comment?

comment in r

This article will sum up the knowledge about how to use comment in the R language. The comments make the code more understandable and explained. When evaluating alternative code, the comments may be used to stop execution.

What is the comment in R?

R uses general English statements called comments to make code more readable, prevent code execution, including resources, and explain the code or project metadata. The logic of its code doesn’t have anything to do with comments.

And the comment has the type of comments. 

Type of comments in R:

  • Single-line Comments: need only one line.
  • Multi-line Comments: need a line or more.
  • Documentation Comments: frequently written for a rapid documentation search.

Syntax:

# The comments

How to use this function?

comment in R: Single-line comments

Only single-line comments created with the “#” symbols are supported by R language. Comments that only need one line are referred to as single-line comments.

# Create and view a data frame
df <- data.frame(name = c("Anjelica D. Stephens","Helena P. Birkland","Thomas D. Riding",
                          "Catherine C. Henke","Kyle R. Dumas"),
                 Math = c(7,8,6,5,9),
                 Physics = c(9,8,6,4,8),
                 English = c(8,9,6,3,4),
                 class = c("A","A","B","C","B"))
cat('The data frame is:\n')
df

Output:

The data frame is:
                  name Math Physics English class
1 Anjelica D. Stephens    7       9       8     A
2   Helena P. Birkland    8       8       9     A
3     Thomas D. Riding    6       6       6     B
4   Catherine C. Henke    5       4       3     C
5        Kyle R. Dumas    9       8       4     B

When we run the code, the comment does not impact this example.

# Create and view a data frame
# df <- data.frame(Math = c(7,8,6,5,9),
#                  Physics = c(9,8,6,4,8),
#                  English = c(8,9,6,3,4))
#
# df

comment in R: Multi-line comments

Although the R language does not enable multiline comments, we may nevertheless construct them by simply inserting a # after each line.

# Start code R - this is a one-line comment
# Create a data frame
df <- data.frame(
  Math = c(7,8,6,5,9),
  Physics = c(9,8,6,4,8),
  English = c(8,9,6,3,4)
)

# View data frame
cat('The data frame is:\n')
df
# This one line is not impact

# Create a vector
vec <- c(1:20)

# View vector
cat('The vector is:',vec)

Output:

The data frame is:
  Math Physics English
1    7       9       8
2    8       8       9
3    6       6       6
4    5       4       3
5    9       8       4
The vector is: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Another example, if you remark or uncomment the chosen lines, you can use the “control + shift + C” keyboard shortcut.

Summary

This article shared information about the comment in R. If you have any questions, please leave a comment below. I will answer as possible. Thanks for reading!

Maybe you are interested:

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *