Course Content
R Introduction: Part II
R Introduction: Part II
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.
# 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
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
).
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
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)
Swipe to show code editor
Let's continue working with the store
data frame.
- Remove the
'Dining chair'
row (index 4) out of thestore
data frame. Reassign the result to thestore
variable. - Add a new row to the data frame
store
using thelist
approach with the data below.
Item | Price | Sold |
---|---|---|
Kitchen Cabinet | 70 | 67 |
Output modified data frame.
Thanks for your feedback!
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.
# 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
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
).
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
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)
Swipe to show code editor
Let's continue working with the store
data frame.
- Remove the
'Dining chair'
row (index 4) out of thestore
data frame. Reassign the result to thestore
variable. - Add a new row to the data frame
store
using thelist
approach with the data below.
Item | Price | Sold |
---|---|---|
Kitchen Cabinet | 70 | 67 |
Output modified data frame.
Thanks for your feedback!