Rename columns and dropping other irrelevant columns
Introduction
We have already covered the basics of renaming columns here. If we want to rename columns and drop the irrelevant columns then we use the select(…) function.
Procedure
We will be working with a custom dataframe.
# package for creating dataframe
library(tibble)
# tibble or dataframe with column names to rename
df <- tibble(col_old_1 = as.integer(c(1,2,3)),
col_old_2 = c(5.0,6.3,9.7),
col_old_3 = c("string1", "string2","string3"),
col_old_4 = c(FALSE, TRUE, FALSE),
col_old_5 = as.factor(c("A","B","A")),
)
View(df)
The first few rows are as follows:
We will rename the col_old_1 and col_old_2 columns from the custom dataframe and drop the other columns.
Code
# refer procedure for definition of df
library(dplyr)
# rename the col_old_1 and col_old_2 columns and drop the other columns
results <- dplyr::select(df,
col_new_1 = col_old_1,
col_new_2 = col_old_2)
View(results)
The output of above code is:
Conclusion
Thus we have successfully renamed columns and dropped other columns for a dataframe.
References
- https://r4ds.had.co.nz/