Online Learning Platform

Data Analysis > Reading Data in R > Remove and Rename variables

Discard unnecessary variables from dataFrame

Suppose your loaded dataFrame name is data which are made from "h1.sav" data file descibed in previous sectiomn. If I need to keep some columns: HL3, HL4, HL5Y, HL6, HH6, HH7, HH7A, wscore, ethnicity, helevel, schage  and discard all other columns

     /install.packages("dplyr")

      library(dplyr)

df = data%>% select(HL3, HL4, HL5Y, HL6, HH6, HH7, HH7A, wscore, ethnicity, helevel, schage)

Now data frame “df” is our final selected data.

 

Rename a column name

We can rename a column name using any one of the the following:

  1. Using R base command
  2. Using dplyr library

 

  1. Using R base command

Suppose we want to rename HL4 column to gender:

               names(df)[2] = "gender"

or

               names(df)[names(df) == "HL4"] <- "gender"

if we need to change multiple columns at a time the command is

               names(df)[c(1, 2, 5)] <- c("relation", "gender", "location")

or

             names(df)[names(df) %in% c("HL3", "HL4", "HH6")]

                                  = c("relation", "gender", "location")

2. Using dplyr library

Suppose we want to rename HL4 column to gender:

          //library(dplyr)

          df <- df %>% rename(gender = HL4)

if we need to change multiple columns at a time the command is

df <- df %>% rename(gender = HL4, relation=HL3, locality = HH6 )

>df

Prev
Reading SPSS Data File
Next
Saving data from a DataFrame to Excell
Feedback
ABOUT

Statlearner


Statlearner STUDY

Statlearner