Starting R .windows.net



R Training GuidebookStarting RBefore Class – Getting Set up in R Studio Cloud and Installing R StudioBefore our November 17th and November 19th class sessions, we’ll need to get set up in the R Studio Cloud. On your work/personal computers, you’ll want to use R Studio (for desktop), but for the purposes of this training we’ll use the R Studio Cloud so that the instructor can easily share completed R code with students!Getting Set Up in R Studio CloudSign up for the free plan on the R Studio Cloud site : Free plan and signup with email, Google or GitHub account and Log in.We will conduct all of our classes via RStudio Cloud. This is a cloud based service from RStudio that allows you to run R on your internet browser.After signing up go to the link provided below to open the R Project for the workshop. (Project can take some time to open.)Once you open up the project, save a permanent copy to your workspace by clicking on the “Save a Permanent Copy” button top right. to get familiar with R before class or continue your education beyond the course? The R Studio Cloud offers free primers to learn R: out this R Studio Cheatsheet for navigating R Studio: . Getting Set Up in R Desktop and R StudioR Studio is an IDE (Integrated Development Environment) that enables programmers to write code using a single graphical user interface (GUI). R is the programming language we’re using, and R Studio is the platform where we program with R. I recommend using R Studio Desktop if you wish to continue using R beyond the class.We’ll need to install R and R Studio (or another R IDE)Installing R:Download R HERE.For Windows and Mac: Download the most recent R release - “R-4.0.3.pkg”Open the downloaded exe. File and run.Install the software, accepting all defaults.Installing R Studio Desktop:Download the most recent version of RStudio Desktop: HERE.Open downloaded .exe file and run.This is a good start but include more of this :In your project, select File -> New File -> R Markdown.If prompted to install packages, select Yes.When prompted, save your file as “Part 1 – Creating LAUS Data”Let’s change the layout of our R Studio.In the Menu, select View -> Panes -> Pane Layout.In the top-right dropdown, switch Environment out for Console.Environment should move to the bottom left now.Check Plots in Environment.Select Apply and Ok.Quick Primer on each Panel:The Source panel is where we write R code that we want to save and export in our R Markdown document.The Console panel lets us execute commands and write code that we don’t necessarily want to save in the R Markdown document.The Environment panel lets us view saved extracts, plots, and data connections.The Files, Packages, Help, Viewer panel lets us view project files, active R Studio packages, View the results of commands from the Console and Source panels, and the Help tab lets us find support for R.Select the Settings icon -> Select Preview in Viewer Pane.This instructs R Studio to publish any previews of data output in a new pane, and not in our window.Ctrl-Alt-i creates a new chunk (Cmd-Alt-i on Macs).Chunks are where we write executable code in the Source pane.We can also create chunks by select the Insert button -> RThe Knit button produces the R Markdown document in the Viewer tab of the Environment pane.Now, navigate to the “Part 1 – Creating LAUS Data Finished” for any more instructions.Importing your Course Work folder into ROnce you have your course work or any folder for that matter downloaded on your computer, you will need to upload it to R, if you are using R Studio Cloud. Those using R Studio Desktop will not need to do this and should instead refer to the next section, “Opening Projects through the R Studio Environment”. The steps to upload a folder to R are as follows:Go to the “Files” tabSelect “Upload”Select “Choose File”, Navigate to where your zip file is located, and then Select “Open”The file name should now appear next to the “Choose File” button. Check to make sure that is correct and then select “OK”.If you get the following overwrite warning message, click “Yes”.R will automatically unzip the folder and put it under your “Files” tab. If you see it there then, it has successfully upload. If you do not see it, retrace these steps and make sure you did not miss any.Opening Projects through the R Studio environmentThere are several ways to open an R-Markdown file in R StudioOpening using File -> Open FileUpon opening R, select “File” and then select “Open File”The keyboard shortcut to this action is Ctrl+OUse the “Open File Window to Navigate to the file you wish to open. Then, click “Open”Open file using the “Files” tabSelect the “Files” tab in the appropriate windowNavigate to the folder with your fileClick on your project in the “Files” paneBasic Data Types in RA data type is the classification for a point of data in R. These classifications allow R to understand what type of data is contained within a dataset or variable. A good reminder is that a programming language cannot infer as effectively as we can and sometimes, we need to tell it what exactly is it looking at. A great example can be found in calendar dates. If you were to write in a calendar date entirely numerically (i.e. 9.21 for September 21st), the program might incorrectly assume that you are typing in the number 9.21 rather than a date and will treat it differently. Therefore, it is important to know the basic data types so that you know that the program has classified your data correctly. Below are the basic data types that R uses and what they encompass.CharactersThe character data type is the classification used for strings of letters, words, or numbers not meant for mathematical calculation.Examples of character data types are as follows: “x”, “testing”, “I love R programing”, “Code 3333”NumericThe numeric data type is the classification used for any real number. Specifically, this is the data type you will want to use for numbers with decimal places.Examples of numeric data types are as follows: 9.45, 8.0, -265.65IntegerThe integer data type is the classification used for any integer. This classification is similar to numeric but is used for numbers without decimal points.Examples of integer data types are as follows: 9, 365, -2,580LogicalThe logical data type is the classification used for binary true or false statements.Examples Logical data types are as follows: TRUE, FALSEComplexThe logical data type is the classification used for complex numbers that contain both real and imaginary numbers.An example of complex data types are as follows: 1+4iVariables in RNow that we know about data types, we can put them into practice through the creation of variables. Variables are used to store data in R. To create a variable in R:go to a code chunk (denoted by the gray area) in your R markdown documenttype the name of your variable, followed by a <-, followed by what data you want to store in your variableWhen naming variables DO NOT make variables with spaces in the name. In the event you want a variable name with a space use an underscore, “_”, instead. This rule exists as a result of how R, and most programming languages for that fact, reads code.Example: example <- 3In this example, we stored the number 3 in the variable “example”.You can find any variables you create in the Environment tabRunning code in RThere are several was for you to run R code. You can find all of those options and their shortcuts unde the Run button at the top of your code.Run selected line(s)This action will run either the line of code you are currently on or any and all code that you have highlighted.The shortcut for this action is Ctrl+EnterRun current ChunkThis action will Run all of the code in the chunk you currently occupy.The shortcut for this action is Ctrl+Shift+EnterRun Next ChunkThis action will Run all of the code in the chunk directly after the current chunk you occupy.The shortcut for this action is Ctrl+Alt+NRun Setup ChunkThis action will Run all of the code in the chunk directly before the current chunk you occupy.Run All Chunks AboveThis action Runs all of the code prior to the code chunk you occupy.The shortcut for this action is Ctrl+Alt+PRun All Chunks BelowThis action Runs all of the code subsequent to the code chunk you occupy.Run AllThis action will run all of the code you have written.The shortcut for this action is Ctrl+Alt+RInstalling and activating packages in RNot everything you need to do your calculations will be installed in your R session at the start. As a result, you will need to install and activate packages that contain additional data science tools. To install a new package:Go to a code chunk (denoted by the gray area) in your R markdown documentType install.packages(‘name of the package you wish to install’)Example code: Run the line of codeYou can check if the installation was successful by looking at the packages tab.Now that the package is installed we need to activate it in our R session. We can do so by:Going to a code chunkType library(‘name of the package you want to activate’)Example code: Run the line of codeNow the package should be active, and you can use the new tools it has mon FunctionsreadxlWe use for reading excel documents into R. Documentation on this function can be found at code: variable <- read_excel(filename)as_tibbleWe can use the "as_tibble" command to view a sample of the dataset in the console. As_tibble additionally shows the variable type of each column of data. Documentation for this function can be found at packages/tibble/versions/3.0.4/topics/as_tibble.Example code: as_tibble(dataset)namesWe can use the "names" function to view the full column name for each column of data. For documentation on names() can be found at code: names(dataset)renameWe can use the “rename” function to edit the column titles. Documentation for this function can be found at code: rename(current column name = new column name)separateWe can separate a single column into multiple columns, using the “separate” function. Documentation for this function can be found at code: dataset %>% separate(col=column of interest, into = c(new column1, new column2), sep=’character to separate data point on’)uniqueWe can use the “unique” function to display the unique variables in a column, row, or datapoint. Documentation for this function can be found at code: unique(dataset)recode We can use the “recode” function to rename data points in a dataset. Documentation for this function can be found at code: dataset %>% recode(‘old name’ = ‘new name’)printWe can use the “print” function to print any data we want to the console. Documentation for this function can be found at code: print(data point)classWe can use the “class” function to find the data type of a data point. Documentation for this function can be found at code: class(data point)mutateWe can use the “mutate” function to calculate new data points from other data points. Documentation for this function can be found at code: dataset %>% mutate(new data point = old data point/ old data point2)joinWe can use the “join” function to combine data from two separate datasets into a single dataset. Documentation for this function can be found at code: dataset %>% join(dataset2, by=(connection points))groupbyWe can use the “groupby” function to group data by certain data points. Documentation for this function can be found at code: dataset %>% group_by(column1, column2, column3)Using magrittr piping in RLooking through the section on common functions, you may have recognized that some of the function examples use the combination of characters %>%. These characters in R are used for an action called piping. Piping is used for forwarding an object, such as a variable, into a function or expression. This allows us to cut down on the number of lines of code we need to write, while also improving the readability of the code.Using piping for functionsHere is an example of piping from Part 1:We want to remove all NA values from a dataset. In this example the dataset is named “laucntycur14”. To remove the NA’s from this dataset, we want to apply the drop_na() function to it. We can see this written out in the image below.Using piping for expressionsExpressions are the values input into functions, which is written out as any put in between the parentheses or brackets of a function. The variable is represented in the expression by a period which signifies to R where to pipe the variable.Here is another example from Part 1:We want to pipe the column of month year into the sub function’s expression. Do this we type out the data we want to select, laus_data[[‘month_year’]], and then the piping expression, %>%, and then the function itself placing a period where the data would normally go. We can see all this written out below. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download