Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Manipulating Columns | Section
Practice
Projects
Quizzes & Challenges
Questionários
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
Tarefa

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.

Solução

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 38
single

single

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

close

bookManipulating Columns

Deslize para mostrar o 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
Tarefa

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.

Solução

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 38
single

single

some-alt