R Basics

R Studio Structure

RStudio is structured with panes. There is a panes that is the Console (where R commands are typed and executed), a pane that shows the environment (showing data sets loaded) and other items, a pane showing the Files, Packages, etc, and when a file is open there is a fourth pane showing the open files.

Packages

If not using an rmd file, then you need the following packages turned on in rStudio: mosaic, and any data packages that you are accessing such as NHANES, fivethirtyeight, MASS, or mosaicData prior to starting each session.

If using an rMarkdown (rmd) file, then put these packages in the setup r chunk using library(“mosaic”) for the mosaic package. Use library(“package name”) for other packages.

Executing an r command

If you ar not using an rmd file, then all commands are typed in the Console pane. If you are using an rmd file, then you will insert an r chunk in the rmd file.

Inserting an r chunk

To insert an r chunk in the rmd file, click on the arrow next to the c in the window controls. Then choose r. this will insert an r chunk in your file and you can type all commands in this file. To run the commands, you can click on the green arrow in the r chunk, or press cntr enter on a pc or command return on a mac on the line you want to run.

The advantage of using an rmd file is that if you made a mistake in the command or you want to reuse this command later, then the command structure is already there for you.

Case Sensitivity

Important, rStudio is case sensitive. Make sure you pay attention to the name of the dataset and the variable and type them exactly as you see them, including the capitalization used. If you want to find a mean, type mean. If you type Mean, rStudio will not know what that is. If your variable is called Weight, then you must type Weight. Otherwise you will get an error saying the variable is not found. Same with the name of your datasets. This is a common error, so if you get an error, check that you have the name of the variable and dataset typed correctly.

r commands

The structure of almost all commands in this class is:

command(response_variable ~ explanatory_variable, data=Data_frame, …)

Where command is the command you want r to execute, such as creating a graph, calculating statistics, or computing inferential statistics. The … means that for some commands there are more options such as titling a graph, colorizing a graph, and calculating certain descriptive statistics. The symbol ~ is in the upper left of most keyboards. It can be thought of “is related to.”

Note: the generic names Data_frame and variable are used throughout these commands. A Data_frame should be named something that makes sense for the data, and then use that name in place of the words Data_frame in all commands. For the variable, find the names of the variables used in the Data_frame and use that in place of the word variable. To find the names of the variables, click on the arrow next to the data frame name in the global environment.

Random r Studio commands

You can copy any of the output from rStudio by highlighting it and then ctrl c (command c on a Mac).

The symbol %>% or |> can be thought of “and then” and it allows to use the code on that line in the next line. It is very useful for string together commands and make them more readable. It is also useful for creating visualizations that contain multiple graphs.

Rendering an rmd file

To share the rmd file with others, you can render it or what is called knitting for an rmd file. In the yaml you can set a default output type to knit to. It can be html, word, or pdf. A pdf file has some issues, so I suggest a word file. If you want a pdf file, then export the word file to a pdf file.

Notice in the set up r chunk there is a command for knitr. Make sure this command is in every setup r chunk in an rmd file.

To knit the file, click on the words knit in the rmd file pane toll bar. If you want to knit it to a different format from the default, then click on the arrow next to the words knit and choose the type of file to knit to. Note, make sure that the default directory for knitting is set to Document Directory.