Brandon Gavett
14/3/2022
When using any programming language, making your syntax understandable to yourself and others is essential. There are several resources available to help you write good code.
No need to run R itself - use RStudio (requires R to be installed)
Make sure your software is up to date.
Make sure you are able to install packages.
Run the following command in the RStudio console:
install.packages("car")
If this does not work, and you have an active internet connection, then you probably don't have appropriate permissions on your computer. If using a Uni computer, you will need to have IT assist you.
object <- value
<-
) tells R to store what's on the right of the arrow in the object named on the left side of the arrow.
1stObject
won't work - use firstObject
instead)first object
won't work - use first_object
instead)mean
is a function in R that is used to calculate the mean.
mean
.Once an object is created, it can be viewed or manipulated.
> object
> print(object)
> object + 1
(adds 1 to each element stored in object
)> sqrt(object)
(takes the square root of each element in object
)> object^2
(squares the value of each element in object
)The syntax print(object)
and sqrt(object)
both illustrate another feature of R: functions. We'll cover these more later.
print
and sqrt
are the functions, and object
is what is passed to the function. Try these 12 basic exercises. Type the following commands next to the >
in your console and then press Enter/Return. Make note of what happens.
2 + 2
x <- 5
print(x)
x
y <- 100
y
z <- x + y
z
z - x == y
a_message <- "Case matters!"
a_Message
a_message
Some of the things to look out for when using the RStudio GUI:
Colour-coding (defaults)
Red X next to the line number:
Red squiggles under your code could also indicate a spelling error
A plus sign (+
) in your console instead of the usual >
x <- sqrt(y
bestAnimal <- "dog
Let's see if you can make all of these things appear in your Source code (and then fix them!)
In the top right corner of RStudio, note the icon and text reading, Project: (None)
This will create a new file in your chosen directory that is specific to this particular project. Doing this for all of your R projects will help make your work more manageable.
The hash symbol (#
) should be used to denote comments (R will ignore anything to the right of a #
)
An example of annotating syntax:
2 + 2 # R can do basic math!
x <- 5 # No output was produced.
print(x) # R saved the number 5 as an object named x. That's what the <- (left arrow) does.
x # I can print the contents of x with or without the print command
y <- 100 # R is storing the number 100 as an object named y
y # This lets me see what's stored in y
z <- x + y # Basic addition
z # The object named z is now storing the value 105 (x + y = 5 + 100)
z - x == y # The double equals sign tells R to check whether two values are equal
a_message <- "Case matters!" # We are storing a text value inside an object called a_message
a_Message # This produced an error because the M was capitalized
a_message # This worked because I used the correct case
command
+ shift
+ c
(Mac)Ctrl
+ Shift
+ c
(PC)option
+ shift
+ k
(Mac)Alt
+ Shift
+ k
(PC)You have several options:
Command
+ return
(Mac)Ctrl
+ Enter
(PC)Run
at the top right of the scripting windowCommand
+ return
(Mac)Ctrl
+ Enter
(PC)Run
at the top right of the scripting windowCommand
+ return
(Mac)Ctrl
+ Enter
(PC)Run
at the top right of the scripting windowpacman
package makes package management efficient and easy.install.packages("pacman")
Installing a package in R is only half the battle. The next step is to load it into your active session.
library(pacman)
R does not automatically load every installed package because you may eventually reach the point where you have hundreds of packages installed, and you won't need them all loaded at the same time. Only load the ones you need to keep resource utilization manageable, especially on computers with low RAM.
Now that we have pacman installed, we can use it to manage our packages. Let's try to load the tidyverse
package, which is actually a collection of extremely useful packages. The psych
package has some great tools we may want to utilize, so let's try to load that too.
pacman will load the tidyverse
suite and psych
packages if they're already installed, and if they're not installed, pacman will do that for us.
p_load(tidyverse, psych)
If you want to verify that these packages have been installed and loaded, click on the Packages tab in the lower right quadrant of RStudio. If a package is there, it's installed, and if its box is checked, it's loaded and ready for you to use.
While we're in the packages pane, let's scroll down to find the psych
package. It should have a check next to it indicating that it has been loaded. If not, make sure you ran the command p_load(tidyverse, psych)
and that it executed without any errors.
Click on the word psych
to open its help files.
You will see a table of contents and other information about this package. Each of the Help Pages shown here corresponds to a single function within that package. Take a quick look to see everything the psych
package offers.
rescale
function, which produces standardized scores with a chosen distribution.
x
- the raw scoresmean
- desired mean of the rescaled scoressd
- desired standard deviation of the rescaled scoresdf
- if TRUE, returns a data frame, otherwise a matrixmyScores <- c(101, 130, 65, 87, 119, 102, 74, 96, 101, 88) # Put 10 made up scores into an object called myScores
rescale(x = myScores) # runs the rescale command on the data stored in the myScores object
Using the help file as a guide, try converting these same 10 scores to scaled scores (M = 10, SD = 3) instead of IQ scores.
Next week, we'll go into more detail about how to use R syntax to work with your data and we'll learn to perform some basic statistical analyses.
Anything else you'd like to see covered? Shoot me an email.
Files from this workshop will be posted to GitHub: https://github.com/begavett/Rbasics
The video recording of this workshop will be available on Teams.