Introduction lab worksheet
We invite people to contribute to a website (https://whyiuser.wordpress.com/) where they explain how they use R, how they got started with R, and what the like/dislike about it. Check out the website and read at least two entries.
What are two things that you would like to be able to do with R that will be useful for your school/career plans?
What are your concerns about learning R?
RStudio
First, you should get up and running with RStudio. You can do this on classroom computers by opening the application. If you have a personal computer, download and install RStudio (free!).
RStudio installation tutorial for Windows: https://www.youtube.com/watch?t=2&v=5ZbjUEg4a1g
RStudio installation tutorial for Mac: https://www.youtube.com/watch?t=9&v=buCEFFuLpYo
Watch these Udacity mini videos to learn about
RStudio basics: https://www.youtube.com/watch?&v=FDSmlIBy7ko
adjusting RStudio settings: https://www.youtube.com/watch?&v=vLlj5nNj8x4
getting help with R: https://www.youtube.com/watch?v=ABVX527RODE
A couple additional time-saving tips:
when you’re in the console, you can use the up arrow to go through your
command history
to execute a line of code from your script in the console, put the cursor
anywhere on that line and type command enter
3. Complete the exercise showing which panel can take care of each function at the end of the RStudio basics video (this may be easier to come back to after you’ve used RStudio a bit)
a. Save an R script
b. Review the log of commands entered
c. Read help documentation
d. Clear the workspace
e. Run a bunch of commands from a file
f. Look at a plot
g. See a list of objects in memory
h. Read the results from functions or calculations
Learning R
There are tons of great resources on line to help you learn the basics of R. Here’s a few that might be useful
CodeSchool (extensive interactive intro to R): http://tryr.codeschool.com/levels/1/challenges/1
DataCamp (another extensive interactive intro to R): https://www.datacamp.com/getting-started?step=2&track=r
R tutorial (more of a textbook layout): http://www.r-tutor.com/r-introduction
R for cats (campy. Particularly intro is useful): http://rforcats.net/
Scripts
So far you’ve been using the console to interact with R. For projects, it’s more practical to save R code in a script which you can modify and run many times. Create a new script in R (under the file menu -> new file -> R script, or icon in the top left corner).
Copy some of the commands you’ve already run into the script. Clear your environment of the variables you’ve made so far (broom icon at the top of the environment window). Experiment with running the whole thing (source icon in top of the script window, use source with echo to get the usual feedback in the console).
Clear your environment of variables again. Now experiment with running your script one line at a time (run icon at the top of the script window).
Tip: with your cursor on the line you’d like to run in the script window, you can simply type control-enter to run that line and advance the cursor to the next line.
Comments
Comments are useful for annotating your code so that you and other people can easily understand it. A comment is any text after a pound sign. That text is ignored by the R compiler. For example,
>x=3 # this line of code sets the value of x to 3
9. Use comments to annotate the variables as you created in question 5.
Logical operations
Logical operations are an automated way to check the value of a variable. From above, the variable x is set to the value 3, then we can use logical operators like this:
>x==3 # x equals 3?