Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Manipulating Columns | Section
Practice
Projects
Quizzes & Challenges
Frågesporter
Challenges
/
Essential R Programming for Absolute Beginners - 1768563985826

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
Uppgift

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.

Lösning

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 38
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

close

bookManipulating Columns

Svep för att visa menyn

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
Uppgift

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.

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 38
single

single

some-alt