[1] 2
Free, open source software and programming language
Commonly used in psychology research for data analysis and visualization
We will be using R in this class to do most of our labs and homeworks.
If you have not done so already, please download R from https://cran.rstudio.com/
Download link: https://posit.co/download/rstudio-desktop/
Integrated Development Environment (IDE) for R
Also download the lab1_template.R and lab1.csv files from the course canvas
console on the left side of the screen.(You can ignore the [1] for now)
+, -, *, /, etc. Remember that order of operations apply, and you can parentheses () as well.<-.You’ll notice that this time there is no output but something else changed on your screen (what is it?)
Now, whenever we write x it will take on the value of whatever it was assigned to.
x back to itself in order to update its value.c() around numbers surrounded by commas.[]. This number is known as its index.| 1 | 2 | 3 |
| 2 | 3 | 4 |
| 3 | 4 | 5 |
x y z
1 1 2 3
2 2 3 4
3 3 4 5
As you can see, we first created three vectors x, y, and z. Then, we made a data frame out of those three vectors.
Data frames can also have operations applied to every number
$ symbol and the name of the columnOn the top right of the RStudio, click File –> Open File, and select lab1_template.R.
You should see something that looks like this on the upper left panel:
This is your script
You may run the file one line at a time just like the console by pressing the Run button.
You can also run the entire file at once by clicking on the Source button.
Unlike with the console, this will not display the output automatically. However, variables are still updated as usual.
To display the output of a variable, use the print() function.
# denotes lines which will not be run with the rest of the file.
Is used to make notes or test out code.
Try deleting the # on the last line of code and clicking Source to see what happens.
Oftentimes you need to use data from files.
One common file type you will use are .csv files, which stands for comma separated values.
Try opening the lab1.csv file to see what it looks like.
To make the next part of this presentation work, you must change your current working directory.
Go to Session –> Set Working Directory –> To Source File Location
lab1.csv file by using read.csv() :To be usable by your program, your .csv file must be in the same folder as your .R file.
Try moving your lab1.csv file to a different location and run read.csv() again to see what happens.
You can navigate around your files by using the bottom right panel, which we have been ignoring up until now.
Sometimes the base R doesn’t have all of the tools that we need.
Let’s install psych, which provides functions for psychological research.
library() .describe() vars n mean sd median trimmed mad min max range skew kurtosis se
x 1 9 5 2.74 5 5 2.97 1 9 8 0 -1.6 0.91
y 2 9 9 5.48 9 9 5.93 1 17 16 0 -1.6 1.83
describe() without either installing or importing the library, you will get an error.Download the body_image_data.csv from the course canvas. Then create an R program that: