Modify Data Frames Using R

Learn how to alter columns and rows of data frames.

We need to continually alter the characteristics of rows and columns during the data-wrangling process to enable easier formatting and optimization for subsequent steps. Below are some of the most commonly employed methods in data analysis.

Modify column names

The colnames() command lists the column names. This command returns the column names as vectors of string values. Column names can be altered by assigning new values to the output of the colnames() command.

Note: The number of new column names and the columns must be equal. Otherwise, the columns without labels are automatically represented as NA.

Press + to interact
# The first 5 column of 'iris' dataset
print('Before the change:')
print(head(iris))
print('------------------------------------------------')
# Check the column names of 'iris' dataset
print('Column names:')
print(colnames(iris))
# Assign new column names
colnames(iris) <- c('col1','col2','col3','col4','col5')
# Check the new column names
print('After the change:')
head(iris,5) # View the first 5 rows.
print('------------------------------------------------')
colnames(iris) <- c('col1','col2','col3','col4') # One column name is missing
# Check the new column names
print('A column is left without a name:')
head(iris,5) # View the first 5 rows.

The colnames() function returns string values, meaning that these values can be modified using string functions. Let’s say we have some measurements taken in a specific period and we want to indicate this ...

Get hands-on with 1400+ tech skills courses.