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

Contenido del Curso

R Introduction: Part II

R Introduction: Part II

1. Matrices
2. Data Frames
3. Lists

bookManipulating 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)

Tarea

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 desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 6
toggle bottom row

bookManipulating 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)

Tarea

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 desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 6
toggle bottom row

bookManipulating 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)

Tarea

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 desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

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)

Tarea

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 desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 2. Capítulo 6
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
some-alt