Chapter 10: Where to Go from Here and Common Pitfalls

“The journey of a thousand miles begins with one step.” — Lao Tzu

There are many resources that can aid in developing your R skills from here. We have introduced the basics of R, helping you take a few steps on your journey of understanding R. We have focused on the ones that are most important for researchers in the health, behavioral, and social sciences.

Since this has been a primer, we hope that you will continue your learning of R via the various sources available at little to no cost. Just like this book, many R books are available online as well as in print. This allows you to explore and learn online at your own pace without having to buy a bunch of books or other resources.

Below, we list a few R books that we have found to be useful. Most are available free in some form.

  1. R for Data Science by Hadley Wickham and Garrett Grolemund
  2. Efficient R Programming by Colin gillespie and Robin Lovelace
  3. The R Cookbook
  4. An Introduction to Statistical Learning

There are many, many books that talk about R in various forms so by no means is this a complete list.

Common Pitfalls

To end, we wanted to highlight some pitfalls that can plague any beginner to R. We list a few that we’ve encountered, although others surely exist.

  1. Document your work.
  2. Avoid overriding objects unless it is on purpose. Changing objects can be hard to keep track of in bigger projects.
  3. Ask questions. R is very flexible; this can make it overwhelming to learn since there are many ways to perform the same task. However, there are people who have figured out easy ways to do complex stuff and most are willing to answer an email.
  4. Plan out the steps of your data manipulation and analyses. A few minutes of planning can help you not get lost in the technology and lose sight of the goal.
  5. Understand the statistics before throwing data in a model. This can lead to major problems in science. At the very least, understand the assumptions of the modeling type and when it can and should be used.
  6. Do exploratory data analysis (EDA) to understand your data. R is made for this–so use it. Otherwise, your model may be completely wrong and have many violated assumptions.
  7. Be transparent in your writing. If you use the R scripts correctly, you can provide your code as part of any publication. This will greatly increase replicability of our important research findings.

Quiz

As a final note, we thought we would give you a quiz to test your memory of the topics we’ve covered. Don’t worry; no pressure to get them all. We’ve included some tougher ones. Regardless of how well you do, we hope you’ll continue improving in your R programming skills.

Question 1

What kind of vector is this?

x <- c(10.1, 2.1, 4.6, 2.3, 8.9)

Question 2

What does this line of code do?

df[c(1,5), c("B", "C")]

Question 3

In the tidyverse there are four join functions. What are they?

Question 4

What functions are used in the “three step summary” as described in Chapter 2?

Question 5

What does the following code do?

ggplot(df, aes(x=C, y=D)) +
  geom_boxplot(aes(color = C)) +
  theme_bw() +
  scale_color_manual(values = c("dodgerblue4", "coral2"))

Question 6

Name three functions you can use to summarize your data in an informative way.

Question 7

What type of model does aov() perform?

Question 8

What are the differences between aov() and lm()?

Question 9

What assumptions of normality and heteroskedasticity fail, what function can be used to fit logistic and poisson regressions?

Question 10

If you were trying to perform logistic regression, what arguments are necessary?

Question 11

In multilevel modeling, which functions can be used to fit a Generalized Estimating Equations model?

Question 12

When comparing mixed effects models, what does anova() do?

Question 13

Can R do structural equation modeling? If so, what package(s) are useful?

Question 14

What types of models can glmnet() perform? How can you do a cross-validated “glmnet” model?

Question 15

Is the following data in wide or long form? How do you know?

##    ID  measures   values
## 1   1 Var_Time1  0.09619
## 2   2 Var_Time1 -0.89741
## 3   3 Var_Time1 -0.50998
## 4   4 Var_Time1 -1.45272
## 5   5 Var_Time1 -0.26957
## 6   6 Var_Time1 -0.28832
## 7   7 Var_Time1 -0.32735
## 8   8 Var_Time1 -0.30502
## 9   9 Var_Time1 -0.24524
## 10 10 Var_Time1 -0.58800
## 11  1 Var_Time2  0.60781
## 12  2 Var_Time2  0.24101
## 13  3 Var_Time2  0.55307
## 14  4 Var_Time2  0.52896
## 15  5 Var_Time2  0.81928
## 16  6 Var_Time2  0.09801
## 17  7 Var_Time2  0.58276
## 18  8 Var_Time2  0.38123
## 19  9 Var_Time2  0.90424
## 20 10 Var_Time2  0.78471

To make your data long form but it is currently in wide form, what function(s) can you use?

Question 16

What form of looping is the fastest? What does apply() do? Can you do for loops in R?

Question 17

What is the following code doing?

sandwhich <- function(pb, jam){
  s <- pb + jam
  return(s)
}

Question 18

What is wrong with this chunk of code?

df <- df +
  mutate(newvar = ifelse(oldvar == 1, 1, 0))

Question 19

What is your favorite built in theme or how would you make your favorite custom theme?

Question 20

What kind of plot does the following make?

pos = position_dodge(width = .1)
ggplot(summed_data, aes(x = dep2, y = sed, group = asthma, color = asthma)) +
  geom_line(position = pos) +
  geom_errorbar(aes(ymin = sed - s_se, ymax = sed + s_se), 
                width = .1, 
                position = pos)

Goodbye and Good Luck

I hope this has been a useful primer to get you into R. If you still feel rusty, feel free to go through the book again or look at other online resources. R is very flexible and can ease the data and analysis burden of research. Implement good practices and your work will become easier to track, easier to document, and easier to communicate. Good luck on your journey using R in your research!

Step1 <- of_a_journey(you) %>%
  has(begun)
You <- now_have_seen(aspects, of, R) %>%
  that_can(increase) %>%
  productivity(your)
GoodLuck <- journey(on, your)