Is there a difference putting 'rename' before or after in R?

Screen Link:
https://app.dataquest.io/m/323/data-cleaning-with-r/11/high-school-directory-simplifying-the-data-frame

My Code:

hs_directory<- hs_directory%>% select(dbn, school_name, `Location 1`)%>%rename(DBN=dbn)

What I expected to happen:
Is there a difference putting rename before or after select function? Because I realised if i put the rename function after select function, it causes an error.

What actually happened:

hs_directory is a ['tbl_df', 'tbl', 'data.frame'], but we expected it to be a ['spec_tbl_df', 'tbl_df', 'tbl', 'data.frame'].
2 Likes

I don’t know why it is behaving that way, the outputs of the dataframes are the same. I’ve checked the output of the class of both outputs and they appear to be the same for your code and that of DataQuest solution.

class(hs_directory)

class(hs_directory %>%
    rename(DBN = dbn) %>%
    select(DBN, school_name, `Location 1`)
)

class(hs_directory %>% 
    select(dbn, school_name, `Location 1`) %>%
    rename(DBN=dbn)
)

The output is as follows:

[1] "spec_tbl_df" "tbl_df"      "tbl"         "data.frame" 
[1] "tbl_df"     "tbl"        "data.frame"
[1] "tbl_df"     "tbl"        "data.frame"

The best thing to do here is just to follow the dataquest instructions, select first then rename.


:notebook: Some Things To Note

select() keeps only the variable you specify; dplyr rename() keeps all variables of dataframe intact.

2 Likes