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

Course Content

R Introduction: Part II

R Introduction: Part II

1. Matrices
2. Data Frames
3. Lists

bookManipulating Columns

Let's continue to expand our data frames manipulation arsenal! :)

Next on the line - is adding/deleting columns. First, to add a new column, assign a vector of values to the column with a new name. You can do it by using either name in square brackets or the dollar $ sign. For example, let's add to the people data frame column with Job titles.

12345678910
# Data name <- c("Alex", "Julia", "Finn") age <- c(24, 43, 32) gender <- c("M", "F", "M") # Creating a data frame test <- data.frame(name, age, gender) # Adding new column test$Job <- c('Teacher', 'Doctor', 'Manager') test # Output data frame
copy

You could also add this column using test[,'Job'] <- .... Also, note that the length of the vector of values you add must equal the number of rows in the data frame (i.e., you can not add a column with two values if there are ten rows in the data frame). To remove column(s) out of data frame use subset() function, with the first parameter being data frame, and select = - ..., where ... is the name(s) of column(s) you want to drop. For example, we can drop 'gender' column.

12345678
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 'gender' column subset(test, select = -gender)
copy

And lastly, if you want to change column names, use the same approach as for matrices - colnames() function. Remember, colnames(data) <- new_names is the syntax for this operation.

Task
test

Swipe to show code editor

Given data frame store with the information on items and their prices in a small furniture store. Currently, it looks like this:

itemsprices
Sofa340
Armchair150
Dining table115
Dining chair45
Bookshelf160

You need to transform this table into this:

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.

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 2. Chapter 5
toggle bottom row

bookManipulating Columns

Let's continue to expand our data frames manipulation arsenal! :)

Next on the line - is adding/deleting columns. First, to add a new column, assign a vector of values to the column with a new name. You can do it by using either name in square brackets or the dollar $ sign. For example, let's add to the people data frame column with Job titles.

12345678910
# Data name <- c("Alex", "Julia", "Finn") age <- c(24, 43, 32) gender <- c("M", "F", "M") # Creating a data frame test <- data.frame(name, age, gender) # Adding new column test$Job <- c('Teacher', 'Doctor', 'Manager') test # Output data frame
copy

You could also add this column using test[,'Job'] <- .... Also, note that the length of the vector of values you add must equal the number of rows in the data frame (i.e., you can not add a column with two values if there are ten rows in the data frame). To remove column(s) out of data frame use subset() function, with the first parameter being data frame, and select = - ..., where ... is the name(s) of column(s) you want to drop. For example, we can drop 'gender' column.

12345678
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 'gender' column subset(test, select = -gender)
copy

And lastly, if you want to change column names, use the same approach as for matrices - colnames() function. Remember, colnames(data) <- new_names is the syntax for this operation.

Task
test

Swipe to show code editor

Given data frame store with the information on items and their prices in a small furniture store. Currently, it looks like this:

itemsprices
Sofa340
Armchair150
Dining table115
Dining chair45
Bookshelf160

You need to transform this table into this:

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.

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 2. Chapter 5
toggle bottom row

bookManipulating Columns

Let's continue to expand our data frames manipulation arsenal! :)

Next on the line - is adding/deleting columns. First, to add a new column, assign a vector of values to the column with a new name. You can do it by using either name in square brackets or the dollar $ sign. For example, let's add to the people data frame column with Job titles.

12345678910
# Data name <- c("Alex", "Julia", "Finn") age <- c(24, 43, 32) gender <- c("M", "F", "M") # Creating a data frame test <- data.frame(name, age, gender) # Adding new column test$Job <- c('Teacher', 'Doctor', 'Manager') test # Output data frame
copy

You could also add this column using test[,'Job'] <- .... Also, note that the length of the vector of values you add must equal the number of rows in the data frame (i.e., you can not add a column with two values if there are ten rows in the data frame). To remove column(s) out of data frame use subset() function, with the first parameter being data frame, and select = - ..., where ... is the name(s) of column(s) you want to drop. For example, we can drop 'gender' column.

12345678
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 'gender' column subset(test, select = -gender)
copy

And lastly, if you want to change column names, use the same approach as for matrices - colnames() function. Remember, colnames(data) <- new_names is the syntax for this operation.

Task
test

Swipe to show code editor

Given data frame store with the information on items and their prices in a small furniture store. Currently, it looks like this:

itemsprices
Sofa340
Armchair150
Dining table115
Dining chair45
Bookshelf160

You need to transform this table into this:

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.

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!

Let's continue to expand our data frames manipulation arsenal! :)

Next on the line - is adding/deleting columns. First, to add a new column, assign a vector of values to the column with a new name. You can do it by using either name in square brackets or the dollar $ sign. For example, let's add to the people data frame column with Job titles.

12345678910
# Data name <- c("Alex", "Julia", "Finn") age <- c(24, 43, 32) gender <- c("M", "F", "M") # Creating a data frame test <- data.frame(name, age, gender) # Adding new column test$Job <- c('Teacher', 'Doctor', 'Manager') test # Output data frame
copy

You could also add this column using test[,'Job'] <- .... Also, note that the length of the vector of values you add must equal the number of rows in the data frame (i.e., you can not add a column with two values if there are ten rows in the data frame). To remove column(s) out of data frame use subset() function, with the first parameter being data frame, and select = - ..., where ... is the name(s) of column(s) you want to drop. For example, we can drop 'gender' column.

12345678
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 'gender' column subset(test, select = -gender)
copy

And lastly, if you want to change column names, use the same approach as for matrices - colnames() function. Remember, colnames(data) <- new_names is the syntax for this operation.

Task
test

Swipe to show code editor

Given data frame store with the information on items and their prices in a small furniture store. Currently, it looks like this:

itemsprices
Sofa340
Armchair150
Dining table115
Dining chair45
Bookshelf160

You need to transform this table into this:

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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 2. Chapter 5
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt