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
Oppgave

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 38
single

single

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

close

bookManipulating Columns

Sveip for å vise menyen

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
Oppgave

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 desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 38
single

single

some-alt