Creating and Saving a Script

A script in R is a series of commands and function instructing R how to analyze the data. A script can be save and retrieved later for corrections or modifications.

You can execute code one step at a time in the R Studio Console (lower left window), and this is useful for quick one step math calculations, but if you have a sequence of coding statement, it is more convenient to list all of your coding statements in a script in the window at the upper left and then saving the script for future use. Note that when you enter code into the script window at the upper left, do not begin the line with a > character.

Writing Code in R - Important Notes

Nicknaming Your Data Set, Attaching, and Detaching It

You can give the data set a nickname to reduce typing. For example, if I imported a data set called framstudy, I might nickname it "fram" to reduce typing by including this command after the data is imported:

fram<- framstudy

Once the data set has been imported, you should "attach" the data so that R defaults to performing actions on this particular data set.

attach(fram)

If you finish with an attached data set and want to work with another data set, you should detach the first one > detach(fram)] and then import and attach the new one.

Here is an example showing the first part of a script in the upper left window of R Studio.

Starting a New Script

To start a new script, click on the File tab, then on New File, then on R Script.

Beginng a new script in R

Then type in your coding statements.

Attaching and Detaching the Data Set

Here is a short sample script:

The line that says attach(fram)attaches the data set, basically telling R that subsequent data steps should be executed on this particular data set. At the end of the script, one can use the command detach(fram) to detach it.

# (A comment) Import the framstudy.csv file and call it "fram"
########################

fram<- framstudy # Next attach the file to tell R it is the "go to" file (the default)
attach(fram)
View(fram)
# Determine the mean, median, and distribution of continuous variables
quantile(AGE)
summary(AGE)
# Determine the number and proportion of males and females
table(SEX)
prop.table(table(SEX))
# Make a mistake in case
summary(Age)

Enter the script above into the R editor and save it as follows:

Saving a script in R  

Then execute the script by hitting the "Run" tab repeatedly in the editor. When you get to the "View(fram)" step, the editor will show the data file, but you can return to the script by clicking on the tab for the script at the top of the window.

Enter code into a script

 The output from the executed script will appear in the Console window at the lower left, and it should look like this:

fram<- framstudy
attach(fram)
View(fram)
quantile(AGE)
0% *25% 50% 75% 100%
39   45 *52 *59 **65
summary(AGE)
*Min. 1st Qu. Median *Mean 3rd Qu. Max.
39.00
**45.00 *52.00 52.41 *59.00 65.00
table(SEX)
SEX
*1   *2
19   30
prop.table(table(SEX))
SEX
********1 ********2
0.3877551 0.6122449
summary(Age)
Error in summary(Age) :   object 'Age' not found

Note that there was an error message because we gave the command "summary(Age)", but the variable for age is all upper case (AGE), so R did not find it.

Also note that you can copy the commands and resulting output from the Console and paste them into other applications, such as Word files.