name <- value
or name = value
Using <-
or =
we assign a value to a name and store the name in Environment.
By calling name
we can use the value repeatedly without typing it every time we use it.
For example, in the following code chunk, having assigned c(1,2,3)
to x
makes the code more neat and tidy to get the same result.
c(1,2,3)^2 + 2*c(1,2,3) - sqrt(c(1,2,3))
## [1] 2.000000 6.585786 13.267949
x <- c(1,2,3)
x^2 + 2*x - sqrt(x)
## [1] 2.000000 6.585786 13.267949
ggplot(data = mydata, mapping = aes(x = [x-variable], y = [y-variable]))
not enough to draw a plot?
ggplot()
not enough to draw a plot?
The function ggplot()
is simply to tell R
to prepare a canvas for a plot. It is a declaration that you will draw a plot. What you will draw on the canvas should be specified by geom_xxxx()
functions, e.g. geom_point
for scatterplots, geom_bar
for bar plots, etc.
You might think “…why? I even specified mapping variables there!” Yes, I hear you. But, that is again a part of preparation for drawing a plot. We specify mapping variables in ggplot()
so that it applies to all subsequent geom_xxxx()
functions globally.
%>%
vs. +
.
%>%
vs. +
.
Are you manipulating data by creating a new variable, filtering rows, arranging rows, selecting columns, etc.? Then use %>%
.
Are you working on plots? Use +
.
Remember, plotting is all about adding components of a graphic layer by layer.
knitr::include_graphics("img/grammar-of-graphics.png")
Once you declare your intention to draw a plot by typing ggplot()
, every subsequent layer should be added by +
.
In the end, you will commonly write the code in the following structure:
data %>%
select() %>%
mutate() %>%
summarize() %>% # end of manipulation
ggplot() + # beginning of drawing a plot
geom_whatever() +
facet_wrap() +
theme()
,
?
,
?
The comma ,
is used within a function, and it separates inputs.
Let’s check with mean()
. We’ve seen two uses of mean()
with or without na.rm = TRUE
. If we want to include na.rm = TRUE
we always put a comma in between:
mean(x)
mean(x, na.rm = TRUE)
This question is relevant if you received an error message like the one below:
Solution: Git Pull. In the upper right box of RStudio select “Git” and then click the “Git Pull arrow” as seen in the red box below.
Now you should be able to push.
Why did this error occur?
This error occurs when the repository is changed remotely. In other words, the GitHub repostiory contains content that you do not have on your local file system. For example, if you go to the repository and change the README file and commit your changes, you will receive this error and need to pull the remote changes before you can push your local changes again.
This question is relevant if you received an error message like the one below:
Solution:
Change the top of the code chunk to: {r county-most-nativeam}
. Notice the space between r
and the chunk-name.
Why did this error occur?
Code chunks in RStudio need to be explicitly told what language of code to run. In other programming contexts this information is sometimes referred to as a “shebang”. This is why code chunks begin with {r chunk-name}
. It is important for there to be a space after the r
. If the chunk deviates from this format, you may encounter /bin/sh : 1
error.
This question is relevant if you received the following error when you tried to push:
Next, you git pull (the down arrow in the upper right) and receive the following error: “Automatic merge failed”
When you look at your
.Rmd
, the following lines show up:
<<<<<<<< (HEAD)
# YOUR CODE
============
# REMOTE CHANGES TO CODE
>>>>>>>>>> Commit Hash
like in the image below.
The solution is:
.Rmd
to look like you want it to, knit, and stage it in the “Git” tabgit commit -m "merge fixed"
and press enterSee the picture below for details.
Checklist:
Are all pages appropriately linked on Gradescope?
Is your name (and your team’s name) on your document?
Did you commit >3 times? (Check your repo on GitHub)
Did every team member commit? (Also check on GitHub)
Is every code chunk named?
For tidyverse style guidelines:
%>%
and ggplot layers +
should be followed by a new lineClick here for a full list of tidyverse style guidelines.