Tutorial 13: Coding concepts

This tutorial is all about manipulating spreadsheets/data.frames/tables.


Tutorial 13A: Comments

What are they?

Comments are little notes that you can put inside your code chunks to remind yourself what you are doing.


How to make one

The confusing part about comments is that they also use the # (hash) symbol:


The # symbol in your TEXT AREA means “heading”

Look at your script. You should now know that in the TEXT AREA, the # symbol means “new heading”. (See Tutorial 4 for a reminder)


The # symbol inside a CODE CHUNK means “comment/ignore this line”

However, INSIDE A CODE CHUNK, the # symbol tells R to “ignore” anything after it. This means you can add in notes to yourself about the commands you are running. You need a new # for every line you want to be a commenrt

For example, without the #, R would give an error. But as soon as it’s there, R will ignore the text:


I like to put my comments in little boxes to make things easier to read:

#--------------------------------------
# Calculate the sum 1+1 & assign to x
#--------------------------------------
x <- 1+1


Why comment?

Comments are GREAT. You can put notes to yourself in there, explain the programming bit (allowing your text to focus more on analysis), or leave “how-to” guides.




Website created and maintained by Helen Greatrex. Website template by Noli Brazil