Getting Started with R: Essential Skills and Commands
School
Clemson University**We aren't endorsed by this school
Course
MATH 3020
Subject
Computer Science
Date
Dec 12, 2024
Pages
11
Uploaded by BailiffRoseOpossum17
Activity - Getting Started in R - Sample Solutions Note: You must attend class and submit your work before leaving to earn credit for this learning activity. This activity uses the quiz utility in canvas to collect your responses. » Your final grade on this activity will be based primarily upon participation and attendance. « NOTE: You may be asked to complete a portion of this activity again if your submitted image is incorrect. * The correct answers for any auto-graded questions will be shown immediately upon completing the activity. Individual feedback or sample solutions will be provided for free-response questions as quickly as possible. + Sample solutions are provided in the course schedule on our Canvas home page. Use these to learn the material and skills covered and to help you prepare for exams. e PLEASE ask questions and/or meet with the instructor if there are any concepts you still do not understand! Work through this activity to verify you have R and RStudio functioning properly by: o Adjusting default settings + Using a few basic R commands + Copying plots as images and pasting into Canvas ¢ Clearing the console and other windows How can you tell your work has been graded? There is one question requiring manual grading. We will review and provided feedback on this question as soon as possible. You can determine if we have not graded your submission in two ways. ¢ If you go in through Grades and click on the name of the activity to review your submission, you will see a section called Submission Details on the right-side of the window. It will specify "some questions not yet graded" if we have not reviewed your work. Submission Details: i less than 1 Time: : minute Current 70 out of 100 Score: Kept Score: 70 out of 100 * Some guestions not yet graded « In your Grades you will see a rocket ship next in the grade column. Cetting Started in R $ Learning Objectives o Technology Objective (TLO): Use R to simulate data, analyze data, and utilize functions for various probability distributions. o General R Programming Skills = TLO-0.1: Set your working directory in RStudio. (Here you will use Session - Set Working Directory - Choose Directory.) = TLO-0.2: Clear results from specific windows in RStudio. Important R Commands, Skills, and Concepts
The following commands, skills, and concepts are covered in this activity. Use the help function to learn more about the default values and options for any particular function. The first two skills are the most important for this activity. The other commands are used here for testing your RStudio installation and will be discussed again as we learn more about R. « Setting your working directory and, to a lesser degree, changing global options ¢ Clearing the Console and other windows e Colon operator, : o Combine function, ¢() e Mean function, mean() ¢ Round function, round() ¢ Printing results to the Console window » Boxplot function, boxplot() Changing the Global Options in RStudio Complete the following to have RStudio start fresh each time you open it. 1. Open RStudio 2. Select Tools from the top menu 3. Choose Global Options 4. In the General tab, UNCHECK the boxes for the following (red arrows in the image below) o Restore most recently opened project at startup Restore previously open source documents at startup o Restore .RData into workspace at startup (leave Save workspace to Ask in the next line) o Always save history (even when not saving .RData) 5. Click APPLY (but do no close these options yet). (o] Options e Graphics Advanced | Code R Sessions R version: = : : — onspls | (64-bit] C:AProgram Files\R\R-4.2.1 | Change.. 1 Appearance Default working directory (when not in a project): Pane Layout J = Browse... Packages Restore most recently opened project at startup _| Restore previously open source documents at startup R Markdown Workspace Python | > Restore .RData into workspace at startup Save workspace to .RData on exit: | Ask v History Spelling _ =:> _| Always save history (even when not saving .RData) ® 2@ L ~ Sweave ¢ Notes: « | prefer the above settings because it can be a bit dangerous to start a new session with previously stored results and it will be good for your learning to start with a fresh session each time you work in RStudio this semester. « In the next question you will learn to set your working directory manually using the menu, however, you can permanently set your working directory using the Browse button outlined in purple above (once you determine the location you wish to use for this course). Turn Off the Environment Window 1. While still in the Global Options (or reopening by going to Tools - Global Options), Choose Pane Layout from the left menu. 2. UNCHECK the box next to Environment (red arrow in the image below). 3. Click APPLY and then OK to close the Global Options.
Options Choose the layout of the panels in RStudio by selecting from the controls in each . General panel. Add up to three additional Source Columns to the left side of the layout. When a column is removed, all saved files within the column are closed and any Code unsaved files are moved to the main Source Pane. > Console Q__ Add Column 3 N ' . T Appearance [ Source v || | [History, Connections, Build, VC: v | Sru t . Pané Layout :v) JEny ronment | History Files P | Packages foe | Connections @ R Markdown p ackages p Help Python (Z18uild ) | VCS @~ sweave L ) R rsaca = ' Nl N % Spelling | Console v || || Files, Plots, Packages, Help, Vi¢ v | Environment - W cvsn () Mistory | Files @ Publishing /| Piots | Connections - Terminal | Packages v Help 6 Accessibility Pl VCS hd — e Tidncial OK Cancel Apply Notes: « | prefer the above setting because relying on the Environment window can cause you to have rounding errors in your final answer! ¢ |tis important to print results to the console output window, which you will learn to do shortly. Question 1 Setting your Working Directory Now you will learn to set your working directory to a good location. » Before continuing, | suggest creating a folder on your computer which you will use for working in R for this course. + The working directory is where R will save files and where it will look first when importing files. To do this: ¢ Go to the Session tab in the top menu + Select Set Working Directory and then click on Choose Directory « Browse to the directory you want to use and click Open Complete this process in RStudio and review the code created in the Console window (the tab labeled Console). Console (R R42S R versic Copyrigt N1 arFarn QUESTION: Which of the following is most similar to the code created by this process? ¢ Note: The information between the quotations will be different. It is the COMMAND out front that we are interested in. Correct Answer:
o setwd("C:/Users/Username/Desktop") R Programming Tip - Setting your Working Directory ¢ Important: You will need to set your working directory during any R session for which you wish to import data or export results. « If you create a specific folder for working in R for MATH 3020 this semester, you can use any of the following methods: o The menu structure as indicated in the previous question: Session - Set Working Directory - Choose Directory then browse to the directory you want to use and click Open. o Copy the code generated from setting the working directory using the menu and save it for future. You can run that code at the beginning of each session (instead of going through the menu process). o OR Set your working directory permanently as mentioned in the section above using Tools - Global Options. ¢ It is VERY important to know where R is saving files! Question 2 Working in the Console Window For this activity, we will work in the Console window, which is where RStudio "runs" our code (in our next R Activity, we will learn about using scripts in R). Some Tips for Working in the Console ¢ Use the UP ARROW key to recall previous commands ¢ Pressing the TAB key after starting a command will show you possible commands and information about the syntax. This can be VERY useful. In RStudio: Type data and press the TAB key to see something like the following image. o Pay VERY careful attention to the information between the braces {} (which are removed in the image) and then answer the question below. * Note: The associated R package for a given command is indicated in braces {} to the right of that command in the resulting list. > datg] | ¢ data.class [ ? | ¢ data.entry { ? | ¢ data.frame { ? | ¢ data.matrix { ? 11 ¢ dataentry { ? o Go through this process as many times as required for you to feel confident in your answer! Ask questions if you are unsure. ¢ Once you have answered this question, you can delete the word "data". You don't need to run any command here. o Our goal with this question is simply to learn how the TAB key can help you determine what commands are related to the text you have entered. o Tip: This can help you remember the correct command when you have an idea but don't recall the exact command. QUESTION: Which of the following commands DOES NOT come from the {base} package? Correct Answer: ¢ data.entry Question 3 What does this command do? Enter or copy/paste the command below into the console window then press enter/return.
12:17 QUESTION: Choose the answer that best describes the result Correct Answer: ¢ Lists the integers from 12 to 17 Question 4 Find the Mean of a Given Set of Values Type or copy/paste the commands below into the console window, one line at a time. Press enter/return after each command to run that command. The commands will store the given data into an object called "x", print the list of data stored in "x" to the console, and then round the mean of the given data to 2 decimal places. x <- ¢(73.5,77.4,75.4,73.0,72.2,73.2) X round(mean(x),2) e The command x <- will store the set of data into an object named "x". e The command "c" stands for combine. ¢ Typing x after the first command will display the information stored in "x" to the console window. The command "mean" finds the average (or mean). (We will learn about the mean in Lesson 3.) ¢ The "round™ command will round values to the requested number of decimals, indicated here by *, 2)" at the end of the code. ¢ Notes: o Some functions will work without the "¢" command to combine data but others will not. It is best to be safe and combine data using the "¢ command. o This is only one of MANY ways to work with a single set of values (one variable). We will learn other ways to work with data, from a single variable to more complex datasets. QUESTION: Enter the exact value given last in the Console window when you run the above commands. Sample Output > x <- ¢(73.5,77.4,75.4,73.0,72.2,73.2) > X [1] 73.5 77.4 75.4 73.0 72.2 73.2 > round(mean(x),2) [1] 74.12 When Creating Graphs and Complex Output in RStudio - Full Screen is Best! Be sure the graph window in RStudio is sized appropriately and that your resulting graph is clear and similar to the examples of "Good Graphs" provided in the next question. + The error shown below is very common! Learn to recognize it. Fix this issue by making the plot window in the lower right corner of RStudio larger!
€ RStudio ® - O X File Edit Code View Plots Session Build Debug Profile Tools Help O /g 2~ % # Go to file/function v Addins ~ K| project: (None) ~ Console Terminal Background Jobs = R R421 . E/RStufi/ > boxplot(c(63, 65, 68, 72, 75, 72)) Error in plot.new() : figure margins too large In addition: warning messages: 1: In doTrycatch(return(expr), name, parentenv, handler) : display 1ist redraw incomplete 2: In doTrycatch(return(expr), name, parentenv, handler) : invalid graphics state 3: In doTrycatch(return(expr), name, parentenv, handler) : invalid graphics state Files Plots Packages Help Viewer Presc ... ™| = P zoom | Bepotr O | §F S~ $ figure margins too large ¢ Itis best to have RStudio maximized to full screen when creating plots, as shown below. @ Rstudio B - 8 X File Edit Code View Plots Session Buid Debug Profie Tools Help O -opla- i A Goto fie/function - Addins = B project: (None) Console Terminal -~ Background Jobs (5 Envionment History Connections Tutorial =0 2.1 - E/RStuft/ 2 I | [ import Dataset = | ) s6mB ~ | List ~ > ]boxplm:(c(72. 73, 75, 78, 79, 82)) RS ) G e > Environment is empty Files Plots Packages Help Viewer Presentation i) A z00m | Bepgot~ 0 | & %~ Question 5 Creating a Boxplot Now that you have stored the data into an object called "x" in the previous question, you can create a graph of the data using the following command. Type or copy/paste the command below into the Console window in RStudio and press enter/return. Running this command will create a boxplot of the data stored in "x". (We will learn about boxplots in Lesson 3.) boxplot(x) Important:
o Be sure the graph window in RStudio is sized appropriately and that your resulting graph is clear, has reasonable margins, and the fonts are readable, etc. ¢ Graphs in RStudio are created relative to the size of the plot window on your machine. ¢ ltis best to have RStudio maximized to your computer screen and to drag the plot area to a reasonable size. Example of R Studio Window "In Action" with Reasonable Plot Window Size: @ Rstudio ® - o X File Edit Code View Plots Session Buid Debug Profie Iools Help O .oyl = A Goto file/function ~ Addins ~ ) Project: (None) ~ Console Terminal - Background Jobs R R421 . E/RStuft/ > boxplot(c(72, 73, 75, 78, 79, 82)) > )] Files Plots Packages Help Viewer Presentation =T P zoom | Bepot - | 0 | % - GOOD GRAPHS Notice the size of the font relative to the plot as well as the size of the plot. ¢ Note: It is possible that you could have outliers indicated by circles on either the lower or upper ends of the boxplot (or both) as shown in the middle and far-right plots below.
77 76 75 74 | 73 72 Clearing Results is a Useful Skill! To clear the Console window use one of these options: ¢ Go to Edit in the top menu and choose Clear Console e Use the keyboard shortcut, CTRL-L (for Windows) e Click on the broom icon ( 4 ) in the upper right of the console window. It can also be useful to use the broom icon in other windows, such as to clear the History and Plots. Give it a try!! This task covers: e TLO-0.2: Clear results from specific windows in RStudio. DO NOT RELY on the Environment Window for Calculations! « If you correctly completed the steps in Changing the Global Options in RStudio under Turn Off the Environment Window then you should never see the Environment window! ¢ However, to be absolutely clear, you should never rely on the results shown in the Environment window in this course! o Do NOT rely on the environment window for calculations. o The environment window will sometimes show rounded values which may cause you to provide incorrect answers! * We have seen one example already of how to print results to the console window. This is the preferred method for this course. When you submit R code, your program must print ALL results to the console when we test your code.
€ RStudio @ — O X File Edit Code View Plots Session Build Debug Profile Tools Help O - R 2~ i » Go to file/function v Addins ~ R Project: (None) ~ Console Terminal Background Jobs = jronment History Connections Tutorial R RA421 . E/RStuft/ o Mg~ import Dataset ~ | ) 136MiB v & - > x <- mean(rnorm(50, 5, 2)) R~ | ol > X [1] 4.939797 values > X 3979749684123 Files Plots Packages Help Viewer Presentation | = Export ~ Question 6 QUESTION: You should ALWAYS use the environment window to determine values in RStudio. Correct Answer: o FALSE Comments: * You should ALWAYS print results to the Console window for assignments, particularly when you submit code which we might need to run ourselves to check your work. + When finding the mean, we were able to print the stored data to the console by typing the name of the object we created. ¢ Avoid using the environment window as your primary method of finding values. If you correctly setup the Global Options, you should not see the Environment window! Closing RStudio When you are finished with this activity, close RStudio. In the pop-up box, click on Don't Save.
Q RStudio [1B — O File Edit Code View Plots Session Build Debug Profile Tools Help o - 2~ k2 A Go to file/function ~ Addins ~ Kl project: (Nane) = Console Terminal Background Jobs = R R42.1 . E/RStuff/ > Files Plots Packages Help Viewer Presentation e | Quit R Session (B X -%4 Export * 0 Save workspace image to E:/RStuff/.RData? —> Save —> Don't Save Cancel When closing RStudio in our course, there is no need to save your workspace image as a .RData file. You can always click on Don't Save when asked about saving your workspace image. Question 7 QUESTION: When closing RStudio in this course, you will NEVER need to save the workspace image as a .RData file. Correct Answer: « TRUE Comments: ¢ You will never need to save your workspace into a .RData file for this course. Saving R Scripts In your next R Activity, you will learn to create and work with an R Script. o For working in RStudio in our course, you will simply need to save your R Script to submit your work and to preserve your code for future use.