Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Manipulating Rows | Section
Practice
Projects
Quizzes & Challenges
Quizze
Challenges
/
Essential R Programming for Absolute Beginners - 1768563985826

bookManipulating Rows

Just like columns, rows in a data frame can also be added or removed.

Adding Single Rows

To add a single row, you can assign a new entry to the next available row index (nrow(data) + 1). Since a data frame can hold different types of values, the new row must be provided as a list (or a data frame).

Note
Note

By default, text values may be converted to factors when creating data frames. This can cause problems when adding new rows. To avoid this, set stringsAsFactors = FALSE when creating the data frame.

Example

123456789
name <- c("Alex", "Julia", "Finn") age <- c(24, 43, 32) gender <- c("M", "F", "M") job <- c('Teacher', 'Doctor', 'Manager') test <- data.frame(name, age, gender, job, stringsAsFactors = FALSE) # Add a new row test[nrow(test) + 1, ] <- list('Angela', 35, 'F', 'Accountant') test
copy

Adding Multiple Rows

Another way to add rows is by merging two data frames with the same columns using the merge() function. This allows you to add multiple rows at once.

Example

123456789101112
name <- c("Alex", "Julia", "Finn") age <- c(24, 43, 32) gender <- c("M", "F", "M") job <- c('Teacher', 'Doctor', 'Manager') test <- data.frame(name, age, gender, job, stringsAsFactors = FALSE) # New row as data a frame new_person <- data.frame('Angela', 35, 'F', 'Accountant') colnames(new_person) <- colnames(test) # Merge merge(test, new_person, all = T)
copy

Removing Rows

To remove rows, use negative indices inside square brackets, like with matrices.

Example

12345678
name <- c("Alex", "Julia", "Finn") age <- c(24, 43, 32) gender <- c("M", "F", "M") job <- c('Teacher', 'Doctor', 'Manager') test <- data.frame(name, age, gender, job, stringsAsFactors = FALSE) # Remove a row test[-1, ]
copy
Aufgabe

Swipe to start coding

You have a data frame store that contains information on items, their prices, and their quantities sold in a small furniture shop.

Your task is to:

  1. Remove the 'Dining chair' row (index 4) out of the store data frame. Reassign the result to the store variable.

  2. Add a new row to the data frame store using the list approach with this data:

    ItemPriceSold
    Kitchen cabinet7067
  3. Output modified data frame.

Lösung

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 39
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

close

bookManipulating Rows

Swipe um das Menü anzuzeigen

Just like columns, rows in a data frame can also be added or removed.

Adding Single Rows

To add a single row, you can assign a new entry to the next available row index (nrow(data) + 1). Since a data frame can hold different types of values, the new row must be provided as a list (or a data frame).

Note
Note

By default, text values may be converted to factors when creating data frames. This can cause problems when adding new rows. To avoid this, set stringsAsFactors = FALSE when creating the data frame.

Example

123456789
name <- c("Alex", "Julia", "Finn") age <- c(24, 43, 32) gender <- c("M", "F", "M") job <- c('Teacher', 'Doctor', 'Manager') test <- data.frame(name, age, gender, job, stringsAsFactors = FALSE) # Add a new row test[nrow(test) + 1, ] <- list('Angela', 35, 'F', 'Accountant') test
copy

Adding Multiple Rows

Another way to add rows is by merging two data frames with the same columns using the merge() function. This allows you to add multiple rows at once.

Example

123456789101112
name <- c("Alex", "Julia", "Finn") age <- c(24, 43, 32) gender <- c("M", "F", "M") job <- c('Teacher', 'Doctor', 'Manager') test <- data.frame(name, age, gender, job, stringsAsFactors = FALSE) # New row as data a frame new_person <- data.frame('Angela', 35, 'F', 'Accountant') colnames(new_person) <- colnames(test) # Merge merge(test, new_person, all = T)
copy

Removing Rows

To remove rows, use negative indices inside square brackets, like with matrices.

Example

12345678
name <- c("Alex", "Julia", "Finn") age <- c(24, 43, 32) gender <- c("M", "F", "M") job <- c('Teacher', 'Doctor', 'Manager') test <- data.frame(name, age, gender, job, stringsAsFactors = FALSE) # Remove a row test[-1, ]
copy
Aufgabe

Swipe to start coding

You have a data frame store that contains information on items, their prices, and their quantities sold in a small furniture shop.

Your task is to:

  1. Remove the 'Dining chair' row (index 4) out of the store data frame. Reassign the result to the store variable.

  2. Add a new row to the data frame store using the list approach with this data:

    ItemPriceSold
    Kitchen cabinet7067
  3. Output modified data frame.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 39
single

single

some-alt