Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Manipulating Columns | Section
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
Opgave

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 alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 38
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

close

bookManipulating Columns

Stryg for at vise menuen

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
Opgave

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 desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 38
single

single

some-alt