1. Use Text Editor

You can type in command directly in the command window (lower left), but for any analysis of data, you should write a script in the text editor (top left), and save your script.

R has built-in Editor

R-studio has built-in editor, which can send command to the console by Ctrl+R. You can send it line by line, or by region.

You can use any text editor you like

Alternative to above is to use external text editor like Notepad. If you use the external editor, you can copy and paste your code to console.

2. Comment Your Code

In R , everyting that comes after # in each line will be ignored. This is to be used as a commenting tool. For each code that you are not too familiar with, you should insert brief comment, to remind you what that line of code does.

# Sample script for R
X = rnorm(100)   # random sample of 100 numbers from N(0,1) distribution.
X                # see what's inside X
hist(X)          # plot histogram of X
plot(X)          # plot scatter plot of X

In R, there is no support for comment multiple lines at once. So if you are putting long comment, you must put # at the beginning of each line.

If you are using R-studio, you can comment block with Control + Shift + C.

Try to keep your comment short and to the point.

3. Keep Your Code, Not Workspace

You can save your script by clicking on the save bottun, or going to FILE -> SAVE. Default extention is .R, but since script is a text file, you can use any other extention such as .txt.

You should keep all the codes you write for future references. In many parts of data analysis, you will be applying same methodology for different datasets. If you have well documented code saved in organized folder, and can quickly go back to the codes you used before, that will save you tons of time.

In general, it is better to save the code that can quickly reproduce the result from the original code, than to save the workspace (R will ask you when you colose) and try to come back later. Chances are that you will not be sure exactly where you were in your code.

If the code takes long time to run

Sometimes the code takes long time to run. In some Machine Learning model fitting it can take several minutes or sometime hours. In that case, it is better to keep both code and the workspace.

4. Small Coding Tips


Up arrow to recall command

On R console, You can press Up arrow key to recall all the past commands.

In R, leftarrow and equal sign is the same

# These two lines means exactly the same thing
X <- 7
X = 7


You can use ; to conbine two lines of code into one

For example,

X = 7
Y = X*10

Does the same thing as

X = 7;   Y = X*10

If you don’t put ;, R will give you an error. This should not be used too often, but can be used to save space if you have short repetitive codes.

Code Only

# Sample script for R
X = rnorm(100)   # random sample of 100 numbers from N(0,1) distribution.
X                # see what's inside X
hist(X)          # plot histogram of X
plot(X)          # plot scatter plot of X

# These two lines means exactly the same thing
X <- 7
X = 7

# Three lines of code
X = 7
Y = X*10
Y

# Does the same thing as
X = 7;   Y = X*10; Y