Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Manipulating Rows | Data Frames
R Introduction: Part II
course content

Зміст курсу

R Introduction: Part II

R Introduction: Part II

1. Matrices
2. Data Frames
3. Lists

book
Manipulating Rows

Now let's learn how to add/delete rows. Let's consider two methods. The first is more applicable for adding a single row.

The method is to assign a new row to the last plus one index of an existing data frame (to get the number of rows, use the nrow() function). Remember that you can not store data of different types using vectors. So, you need to assign either a data frame or a list with new values. Do not worry if you do not know lists. In simple words, this is like a vector that allows us to store data of different types. Let's represent that. The initial data frame is shown below.

123456789
# Creating a data frame 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) # Adding new row test[nrow(test) + 1, ] <- list('Angela', 35, 'F', 'Accountant') test # Output data frame
copy

Note

By default, when changing a data frame, any new string values in a list are automatically converted to factors. To prevent this automatic conversion, the parameter stringsAsFactors = FALSE should be specified during the creation of the data frame. This approach should be applied whenever you are modifying rows in the data frame.

You could do this by using a new data frame and the merge function. This method requires the same column names and setting of necessary parameters (all = T).

1234567891011
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) test[nrow(test) + 1, ] <- list('Angela', 35, 'F', 'Accountant') # Data of new person as data frame new_person <- data.frame('Angela', 35, 'F', 'Accountant') colnames(new_person) <- colnames(test) # Set column names merge(test, new_person, all = T) # Merge data frames
copy

As you can see, the outputs are identical. To remove rows out of the data frame, use square quotes and put a minus sign to the left of the row index. For example, test[-1,] will remove the first row (the same as for matrices)

Завдання
test

Swipe to show code editor

Let's continue working with the store data frame.

  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 the data below.
ItemPriceSold
Kitchen Cabinet7067

Output modified data frame.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 6
toggle bottom row

book
Manipulating Rows

Now let's learn how to add/delete rows. Let's consider two methods. The first is more applicable for adding a single row.

The method is to assign a new row to the last plus one index of an existing data frame (to get the number of rows, use the nrow() function). Remember that you can not store data of different types using vectors. So, you need to assign either a data frame or a list with new values. Do not worry if you do not know lists. In simple words, this is like a vector that allows us to store data of different types. Let's represent that. The initial data frame is shown below.

123456789
# Creating a data frame 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) # Adding new row test[nrow(test) + 1, ] <- list('Angela', 35, 'F', 'Accountant') test # Output data frame
copy

Note

By default, when changing a data frame, any new string values in a list are automatically converted to factors. To prevent this automatic conversion, the parameter stringsAsFactors = FALSE should be specified during the creation of the data frame. This approach should be applied whenever you are modifying rows in the data frame.

You could do this by using a new data frame and the merge function. This method requires the same column names and setting of necessary parameters (all = T).

1234567891011
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) test[nrow(test) + 1, ] <- list('Angela', 35, 'F', 'Accountant') # Data of new person as data frame new_person <- data.frame('Angela', 35, 'F', 'Accountant') colnames(new_person) <- colnames(test) # Set column names merge(test, new_person, all = T) # Merge data frames
copy

As you can see, the outputs are identical. To remove rows out of the data frame, use square quotes and put a minus sign to the left of the row index. For example, test[-1,] will remove the first row (the same as for matrices)

Завдання
test

Swipe to show code editor

Let's continue working with the store data frame.

  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 the data below.
ItemPriceSold
Kitchen Cabinet7067

Output modified data frame.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 6
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt