336-5 One kind of indexing works but another doesn't?

Hello,

I managed to complete lesson 5 of Working with Data Frames, but I noticed something confusing:
Running this code to index the Major column into college_majors wasn’t accepted:
college_majors <- recent_grads[, "Major"]

But this was:
college_majors <- recent_grads$Major

Am I missing something here about the syntax for the first method of indexing in the readr package?

I was able to progress nonetheless because I used the second method, but I was curious.

Thank you!

Mission Link: https://app.dataquest.io/m/336/working-with-data-frames/5/indexing-data-frames

This is one of those cases where it’s less that your answer is wrong per se, and more that the type of your answer doesn’t match what the answer checker expects.

college_majors <- recent_grads[, "Major"]

Your code above outputs the college_majors variable as a single-column DataFrame.

The following two lines of code on the other hand will output college_majors as a character vector, and therefore pass the answer check.:

college_majors <- recent_grads$Major
college_majors <- recent_grads[["Major"]]

It helps to troubleshoot these issues using the typeof(college_majors) function!

3 Likes

thank you, I had the same question.