Today we will…
R
+ RStudioR
Basics & TroubleshootingHi, I’m Dr. C!
I am a transplant to the west coast.
My favorite things right now are cooking, knitting, and biking around SLO county.
I genuinely love R
.
I am looking forward to reading your introductions on Discord!
R
skillsWork with other’s!
You should work with other’s on all assignments! The final work you submit for a Lab should be your own.
Exam Conflicts
If you have a known conflict with an exam, please discuss it with me at least three weeks prior to the exam date
Late Policy
Attendance
Accessibility and Accomodations
Not just syntax
All of these skills improve as the others improve and we will work on all of them in this course!
Work with the person next to you & introduce yourself!
Write a set of instructions to fold a paper airplane
R
is a programming language designed originally for statistical analyses.R
is open sourceR
’s strengths are…
handling data with lots of different types of variables.
making nice and complex data visualizations.
having cutting-edge statistical methods available to users.
R
’s weaknesses are…
performing non-analysis programming tasks, like website creation (python, ruby, …).
hyper-efficient numerical computation (matlab, C, …).
being a simple tool for all audiences (SPSS, STATA, JMP, minitab, …).
The heart and soul of R
are packages.
R
refers to functions that are in the R
software, and do not require a package.Importantly, R
is open-source.
R
, like there is for SAS or Matlab.R
packages live on the Comprehensive R Archive Network, or CRAN.Being a good open-source citizen means…
R
for ethical and respectful projects.Let’s get into it!
If you have RStudio installed on your computer, open it now.
Otherwise, open the Posit Cloud Project
RStudio is an IDE (Integrated Developer Environment).
R
.You will always interact with R
through RStudio
Helps with organization and some “point-and-click” options if desired
A directory is just a fancy name for a folder.
Directories are your friends!
Best practice:
Create a directory for this class!
Is it in a place you can easily find it?
Does it have an informative name?
Are the files inside it well-organized?
Warning
I cannot stress how important this is!!
Open our “starter” notes to follow along
If on your computer: Download and save w1-notes.qmd on your computer and open it in RStudio.
If on Posit Cloud: Open “w1-notes.qmd”
R
scripts or notebooks in Source
To install a package use:
To load a package use:
Warning
Note that when you install packages you need to include quotation marks around the package name, but you don’t need to when loading a package!
A value is a basic unit of stuff that a program works with.
Values have types:
Variables are names that refer to values.
A variable is like a container that holds something - when you refer to the container, you get whatever is stored inside.
We assign values to variables using the syntax object_name <- value
.
some_people_use_snake_case
somePeopleUseCamelCase
some.people.use.periods
variables_thatLookLike.this
and they are almost universally hated.Tip
Just pick one and stick with it!
Homogeneous: every element has the same data type.
Vector: a one-dimensional column of homogeneous data.
Matrix: the next step after a vector - it’s a set of homogenous data arranged in a two-dimensional, rectangular format.
Heterogeneous: the elements can be of different types.
List: a one-dimensional column of heterogeneous data.
Dataframe: a two-dimensional set of heterogeneous data arranged in a rectangular format.
We use square brackets ([]
) to access elements within data structures.
We can combine logical statements using and, or, and not.
(X AND Y) requires that both X and Y are true.
(X OR Y) requires that one of X or Y is true.
(NOT X) is true if X is false, and false if X is true.
R
is designed to do vector and matrix math nicely
Many operations in R
are vectorized.
R
is great?Loops be gone!
Now forget about loops after this slide! We rarely need them in R
and will avoid using them in this class
seq(from = 1, to = 10, by = 1
Error in parse(text = input): <text>:2:0: unexpected end of input
1: seq(from = 1, to = 10, by = 1
^
seq(from = 1, to = 10 by = 1)
Error in parse(text = input): <text>:1:23: unexpected symbol
1: seq(from = 1, to = 10 by
^
sequence(from = 1, to = 10, by = 1)
Error in sequence.default(from = 1, to = 10, by = 1): argument "nvec" is missing, with no default
sqrt(‘1’)
Error in my_obj(5): could not find function "my_obj"
Just because you see scary red text, this does not mean something went wrong! This is just R
communicating with you.
Often, R
will give you a warning.
This means that your code did run…
…but you probably want to make sure it succeeded.
Does this look right?
If the word Error appears in your message from R
, then you have a problem.
R
what are you trying to tell me??R
says…Error in ggplot() : could not find function “ggplot”
It probably means…
You haven’t installed/loaded the package that includes the ggplot() function OR you mispelled the function name
You should:
library()
)R
says…Error: Object
some_obj
not found.
R
says…Error: Object of type ‘closure’ is not subsettable.
R
says…Error: Non-numeric argument to binary operator.
Look at the help file for the function!
Break what you are doing down into smaller pieces and look at whether or not each step gives you what you expect
When all else fails, Google your error message.
What’s wrong here?
Error in matrix(c("a", "b", "c", "d"), num_row = 2): unused argument (num_row = 2)
Tip
Look up the help file for the function matrix
by running ?matrix
in the Console.
Part One:
This file has many mistakes in the code. Some are errors that will prevent the file from knitting; some are mistakes that do NOT result in an error.
Fix all the problems in the code chunks.
Part Two:
Follow the instructions in the file to uncover a secret message.
Submit the name of the poem as the answer to the Canvas Quiz question.