Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Manipulating Columns | Data Frames
R Introduction

bookManipulating Columns

Data frames are flexible: you can add, remove, and rename columns as needed.

Adding Columns

To add a new column, assign a vector of values to a new column name. You can use either the dollar ($) sign or square brackets with quotes.

Example

12345678
name <- c("Alex", "Julia", "Finn") age <- c(24, 43, 32) gender <- c("M", "F", "M") test <- data.frame(name, age, gender) # Adding a new column with job titles test$job <- c("Teacher", "Doctor", "Manager") test
copy

You could also do the same with:

test[, "Job"] <- c("Teacher", "Doctor", "Manager")
Note
Note

The length of the vector you add must match the number of rows in the data frame.

Deleting Columns

To remove one or more columns, use the subset() function with the select = -... argument.

Example

12345678910
name <- c("Alex", "Julia", "Finn") age <- c(24, 43, 32) gender <- c("M", "F", "M") test <- data.frame(name, age, gender) test$job <- c('Teacher', 'Doctor', 'Manager') # Drop a single column subset(test, select = -job) # Drop multiple columns subset(test, select = -c(age, gender))
copy

Renaming Columns

To rename columns, use the colnames() function. The syntax is the same as with matrices:

colnames(data) <- new_names
Task

Swipe to start coding

You have a data frame store containing information on items and their prices from a small furniture store:

itemsprices
Sofa340
Armchair150
Dining table115
Dining chair45
Bookshelf160

You need to transform it into this table:

ItemPriceSold
Sofa34067
Armchair15081
Dining table11579
Dining chair4576
Bookshelf16042

Follow the next steps:

  1. Rename the columns names of store to c('Item', 'Price').
  2. Add new column Sold with the values of c(67, 81, 79, 76, 42).
  3. Output modified data frame.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 5
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

close

Awesome!

Completion rate improved to 2.27

bookManipulating Columns

Swipe to show menu

Data frames are flexible: you can add, remove, and rename columns as needed.

Adding Columns

To add a new column, assign a vector of values to a new column name. You can use either the dollar ($) sign or square brackets with quotes.

Example

12345678
name <- c("Alex", "Julia", "Finn") age <- c(24, 43, 32) gender <- c("M", "F", "M") test <- data.frame(name, age, gender) # Adding a new column with job titles test$job <- c("Teacher", "Doctor", "Manager") test
copy

You could also do the same with:

test[, "Job"] <- c("Teacher", "Doctor", "Manager")
Note
Note

The length of the vector you add must match the number of rows in the data frame.

Deleting Columns

To remove one or more columns, use the subset() function with the select = -... argument.

Example

12345678910
name <- c("Alex", "Julia", "Finn") age <- c(24, 43, 32) gender <- c("M", "F", "M") test <- data.frame(name, age, gender) test$job <- c('Teacher', 'Doctor', 'Manager') # Drop a single column subset(test, select = -job) # Drop multiple columns subset(test, select = -c(age, gender))
copy

Renaming Columns

To rename columns, use the colnames() function. The syntax is the same as with matrices:

colnames(data) <- new_names
Task

Swipe to start coding

You have a data frame store containing information on items and their prices from a small furniture store:

itemsprices
Sofa340
Armchair150
Dining table115
Dining chair45
Bookshelf160

You need to transform it into this table:

ItemPriceSold
Sofa34067
Armchair15081
Dining table11579
Dining chair4576
Bookshelf16042

Follow the next steps:

  1. Rename the columns names of store to c('Item', 'Price').
  2. Add new column Sold with the values of c(67, 81, 79, 76, 42).
  3. Output modified data frame.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 5
single

single

some-alt