How did it go yesterday?
How much do you feel like you remember?
Ready to keep going?
An R script is a text document that contains lines of R code that can be delivered individually or collectively to the command line.
An R script is a text document that contains lines of R code that can be delivered individually or collectively to the command line.
Scripts are text files, so they can be written, saved, and shared relatively easily.
Open RStudio, go to the File menu, and create a new script. In that script, add some code:
Create a variable called myName and assign your name as a character string
Create a second variable called introduceMe and assign the character string “Hi, my name is”. Be sure to include a space at the end of the character string.
Use these two variables as arguments in the paste
function.
Highlight all of the code in the script and click Run.
Comments are useful for documenting what a piece of code does, both for you and for other users. They can also be helpful for temporarily taking out a piece of code from a script without having to delete it.
The file system is a means of storing and retrieving files on a computer.
The file system is a means of storing and retrieving files on a computer. Many systems are based on a hierarchical collection of directories.
Makes it easier for you to find and update files
Makes it easier for others to understand the logic of the project
Ensures files can be located without user input
Create a file system for saving your work this week. If you’ve already got one going, see how it compares with our discussion so far and think about whether this would be a good time to reogranize.
Think about the following
Will you be able to find it easily?
Will someone else be able to understand how this folder structure works?
Once you’ve done this, go to the File drop-down menu in RStudio and select Save As to store your script inside the appropriate folder for this week’s lecture
Avoid spaces
Avoid special characters
Try and keep them short
Avoid the term final (e.g., code_exercise_3_finalFinal.R)
Above all else… BE CONSISTENT!
Do not modify your raw data manually, or even better, at all.
Data manipulation should work like a conveyor belt: it stops at checkpoints. e.g., it gets modified/cleaned/analyzed, and then it moves on.
Always have collaborators in mind. Work towards shareable code. Have public awareness.
Consistency (within your project) is key.
My recommended approach to problems you might encounter is
try and change a few things, and if that doesn’t work
check the Help documentation, and if the answer isn’t there
search for a solution on the web, and if that fails
ask someone who can help you
Help documents describe what functions do, what arguments they take, and what kind of objects they will return.
You can search help using the Help tab in the Outputs pane.
You can also quickly look up a function’s help document by typing it into the command line preceded by a question mark (?
). For example:
?hist
Help documents include:
Description What does the function do
Usage How do you call it in R?
Arguments What arguments does it take and what objects should be used?
Details Specifics about what different arguments do
Value
Examples Code showing how the code is used
Try looking at the entry for some of the functions we’ve used so far. Examples include:
rep
paste
scan
rnorm
See if you can use help to find the function for the Kolmogorov-Smirnov Test.
Working from the file system: what doing it right looks like
Navigating the high seas of data
More on what to do when things go wrong
Comments
Comments are text preceded by a comment character. For R, the comment character is
#
. When R receives this at the command line, it knows not to run it.