Skip to content
New course: Interactive Dashboards with Shiny. Get 50% off with coupon SHINYLAUNCH.
R for the Rest of Us Logo

Binding Data Frames

This lesson is locked

Get access to all lessons in this course.

Transcript

Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.

Your Turn

  1. Import 2017-2018 enrollment data into a data frame called enrollment_by_race_ethnicity_17_18 and clean it using the code you used for the 2018-2019 data

  2. Use bind_rows() to make a enrollment_by_race_ethnicity data frame.

Hint: You’ll need to change some of your code from importing the 2018-2019 data to make the race_ethnicity variable get recoded correctly!

Learn More

The best place to learn about binding data is the dplyr website.

Have any questions? Put them below and we will help you out!

You need to be signed-in to comment on this post. Login.

Isaac Macha

Isaac Macha

April 22, 2021

when putting a new column, I keep getting this error. How do I solve this :

Error in FUN(left) : invalid argument to unary operator

See below code enrollment_by_ethnicity_18_19 %>% mutate(year = 2018)

David Keyes

David Keyes

April 22, 2021

That's odd. Can you post your full code so I can see what's going on?

Isaac Macha

Isaac Macha

April 22, 2021

I found the solution, the same problem as before, not piping to an object before mutating.

tawheeda wahabzada

tawheeda wahabzada

April 28, 2021

Hi David, in the solutions, you noted that lines 25 and 51 have the missing code. I only see blank spaces.

David Keyes

David Keyes

April 28, 2021

Oops, typo! I've updated it to be lines 24 and 50.

Atlang Mompe

Atlang Mompe

May 2, 2021

Hi David, did you intentionally omit the binding code? In any case I figured it out, but just want to flag that it is not in the solutions. Thanks, Atty

David Keyes

David Keyes

May 3, 2021

Sorry about that! Updated now.

Atlang Mompe

Atlang Mompe

May 3, 2021

Great, thank you David, I have the same code as well.

Christine Vandenberghe

Christine Vandenberghe

May 3, 2021

Sorry I don't see bind rows in the solution, did I miss it?

Christine Vandenberghe

Christine Vandenberghe

May 3, 2021

Sorry disregard, just saw your previous note :)

Maria Cristina Limlingan

Maria Cristina Limlingan

November 15, 2021

HI David,

I'm still getting NAs for race_ethnicity even if I changed the code to this - not sure what else to try? mutate(race_ethnicity = str_remove(race_ethnicity, "x2017_2018_")) %>%

Can you post your full code as a GitHub gist and share the link please?

Try changing the x2017_2018 to x2017_18. I think that's your issue.

Maria Cristina Limlingan

Maria Cristina Limlingan

November 17, 2021

It worked! I'm both glad and (a little) embarrased that it was a simple issue. Appreciate your help! :)

This type of thing is VERY common! Most of my bugs are from small things like this.

JULIO VERA DE LEON

JULIO VERA DE LEON

April 30, 2022

Hi, I ran into a similar issue when we were using the replace_na with "0", in this case for the second dataset (17-18) I received this from the console:

Error in mutate(): ! Problem while computing number_of_students = replace_na(number_of_students, "0"). Caused by error in vec_assign(): ! Can't convert replace to match type of data .

So, I just had to remove the quotation marks from the 0, in order to get it work.

Charlie Hadley

Charlie Hadley

May 3, 2022

Hello Julio,

Thanks for reporting this! It's due to a change in how replace_na() works, it is no longer allowed to "change data types" which means we need to use 0 if the column is numeric and "0" if the column is a character column.

In case you're interested, you can see the documentation for this change in the NEWS.md file for the package. But please note the language used here is quite technical.

Thanks,

Charlie

Andrew Paquin

Andrew Paquin

April 24, 2023

Hi Dave and Charlie, I've run into a problem. I duplicated my code for the 2018-19 data and adapted it for the 2017-18 data. When I run it, however, I get an error message pertaining to line 73 - mutate(number_of_students = na_if(number_of_students, "-")). What puzzles me is that the exact same line works as expected in the original code chunk. Here's the error message: Error in mutate(): ℹ In argument: number_of_students = na_if(number_of_students, "-"). Caused by error in na_if(): ! Can't convert y to match type of x . Run rlang::last_trace() to see where the error occurred. This video (link below) provides a bit more detail: https://www.loom.com/share/4dffa13937994d65bb212ecd544e8e20

Andrew Paquin

Andrew Paquin

April 24, 2023

I should also note that David had that line in his code, and it worked in the videos. That being said, I went to the original files and I noticed two things:

  1. There are no dashes (that I can see) in the 2017-18 file. This may mean that I can eliminate some code for the second set of data.
  2. Whereas the "number of students" columns in the original 2018-19 file are character type, the corresponding columns from the previous year are numeric.

I just deleted the three mutate lines pertaining to the whole na situation, and now it works. I'm still confused why it worked in David's video.

David Keyes

David Keyes

April 25, 2023

It's a bit complicated, but I'll explain the issue in our live session on Wednesday.