Manipulating 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
12345678name <- 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
You could also do the same with:
test[, "Job"] <- c("Teacher", "Doctor", "Manager")
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
12345678910name <- 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))
Renaming Columns
To rename columns, use the colnames() function. The syntax is the same as with matrices:
colnames(data) <- new_names
Swipe to start coding
You have a data frame store containing information on items and their prices from a small furniture store:
| items | prices |
|---|---|
| Sofa | 340 |
| Armchair | 150 |
| Dining table | 115 |
| Dining chair | 45 |
| Bookshelf | 160 |
You need to transform it into this table:
| Item | Price | Sold |
|---|---|---|
| Sofa | 340 | 67 |
| Armchair | 150 | 81 |
| Dining table | 115 | 79 |
| Dining chair | 45 | 76 |
| Bookshelf | 160 | 42 |
Follow the next steps:
- Rename the columns names of
storetoc('Item', 'Price'). - Add new column
Soldwith the values ofc(67, 81, 79, 76, 42). - Output modified data frame.
Рішення
Дякуємо за ваш відгук!
single
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Чудово!
Completion показник покращився до 2.27
Manipulating 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
12345678name <- 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
You could also do the same with:
test[, "Job"] <- c("Teacher", "Doctor", "Manager")
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
12345678910name <- 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))
Renaming Columns
To rename columns, use the colnames() function. The syntax is the same as with matrices:
colnames(data) <- new_names
Swipe to start coding
You have a data frame store containing information on items and their prices from a small furniture store:
| items | prices |
|---|---|
| Sofa | 340 |
| Armchair | 150 |
| Dining table | 115 |
| Dining chair | 45 |
| Bookshelf | 160 |
You need to transform it into this table:
| Item | Price | Sold |
|---|---|---|
| Sofa | 340 | 67 |
| Armchair | 150 | 81 |
| Dining table | 115 | 79 |
| Dining chair | 45 | 76 |
| Bookshelf | 160 | 42 |
Follow the next steps:
- Rename the columns names of
storetoc('Item', 'Price'). - Add new column
Soldwith the values ofc(67, 81, 79, 76, 42). - Output modified data frame.
Рішення
Дякуємо за ваш відгук!
single