Table of Contents
In R, multiple rows can be removed from a dataset using the “subset” function or the “filter” function from the dplyr package. This process involves specifying the criteria for the rows to be removed, such as a specific value or condition, and then using the appropriate function to filter out those rows. For example, to remove all rows with a value of “NA” in a specific column, the subset function can be used with the condition “!= NA”. Other examples include removing rows with a certain range of values or rows that meet a certain logical condition. This process is useful for data cleaning and manipulation, and can be applied to various types of datasets.
Remove Multiple Rows in R (With Examples)
You can use one of the following methods to remove multiple rows from a data frame in R:
Method 1: Remove Specific Rows
#remove rows 2, 3, and 4
new_df <- df[-c(2, 3, 4), ]
Method 2: Remove Range of Rows
#remove rows 2 through 5
new_df <- df[-c(2:5), ]Method 3: Remove Last N Rows
#remove rows 4 through last row new_df <- df[-c(4:nrow(df)), ]
The following examples show how to use each of these methods in practice with the following data frame:
#create data frame df <- data.frame(team=c('A', 'B', 'C', 'D', 'E', 'F'), points=c(99, 90, 86, 88, 95, 99), assists=c(33, 28, 31, 39, 34, 24)) #view data frame df team points assists 1 A 99 33 2 B 90 28 3 C 86 31 4 D 88 39 5 E 95 34 6 F 99 24
Example 1: Remove Specific Rows
The following code shows how to remove rows 2, 3, and 4 from the data frame:
#define new data frame with rows 2, 3, 4 removed
new_df <- df[-c(2, 3, 4),]
#view new data frame
new_df
team points assists
1 A 99 33
5 E 95 34
6 F 99 24Notice that rows 2, 3, and 4 have all been removed from the data frame.
Example 2: Remove Range of Rows
The following code shows how to remove rows in the range of 2 through 5:
#define new data frame with rows 2 through 5 removed
new_df <- df[-c(2:5),]
#view new data frame
new_df
team points assists
1 A 99 33
6 F 99 24
Notice that rows 2, 3, 4, and 5 have been removed.
Example 3: Remove Last N Rows
The following code shows how to remove rows 4 through the last row:
#remove rows 4 through last row
new_df <- df[-c(4:nrow(df)), ]
#view new data frame
new_df
team points assists
1 A 99 33
2 B 90 28
3 C 86 31
Notice that row 4 and all rows after it have been removed.
Additional Resources
Cite this article
stats writer (2024). How can I remove multiple rows in R, and what are some examples of this process?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-remove-multiple-rows-in-r-and-what-are-some-examples-of-this-process/
stats writer. "How can I remove multiple rows in R, and what are some examples of this process?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-remove-multiple-rows-in-r-and-what-are-some-examples-of-this-process/.
stats writer. "How can I remove multiple rows in R, and what are some examples of this process?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-remove-multiple-rows-in-r-and-what-are-some-examples-of-this-process/.
stats writer (2024) 'How can I remove multiple rows in R, and what are some examples of this process?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-remove-multiple-rows-in-r-and-what-are-some-examples-of-this-process/.
[1] stats writer, "How can I remove multiple rows in R, and what are some examples of this process?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.
stats writer. How can I remove multiple rows in R, and what are some examples of this process?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
