 # Drop Variables In R: Drop the rows or columns in a data frame This tutorial will discuss how to drop the rows or columns in the R programming language. If you are interested in this topic, continue reading until the end to find the right solution.

## What is the drop in R?

The subset() function in R can be used to drop the row or column using conditions. For example, let’s look at how to drop rows with several conditions. In R, you can drop the rows or columns by row index (row number) and row name.

## Ways to drop in R

Here, we will create a data frame that is used for all examples as follows:

# Create a data frame
df <- data.frame(
name = c(
"Marnie W. Vivanco", "Augustine D. Salinas", "Amanda R. Goldstein",
"Clarence E. Almodovar", "Alan T. Feuerstein", "Emma J. Lawrence"
),
ID = c(1:6),
Math = c(8, 9, 6, 10, 8, 7),
English = c(8, 10, 10, 8, 9, 9),
Class = c("A", "B", "A", "B", "A", "A")
)

# View a data frame
df

Output:

                   name ID Math English Class
1     Marnie W. Vivanco  1    8       8     A
2  Augustine D. Salinas  2    9      10     B
3   Amanda R. Goldstein  3    6      10     A
4 Clarence E. Almodovar  4   10       8     B
5    Alan T. Feuerstein  5    8       9     A
6      Emma J. Lawrence  6    7       9     A

### Drop rows don’t use function

Now, we will drop the rows in a data frame without using the function as below:

# Create a data frame
df <- data.frame(
Name = c(
"Marnie W. Vivanco", "Augustine D. Salinas", "Amanda R. Goldstein",
"Clarence E. Almodovar", "Alan T. Feuerstein", "Emma J. Lawrence"
),
ID = c(1:6),
Math = c(8, 9, 6, 10, 8, 7),
English = c(8, 10, 10, 8, 9, 9),
Class = c("A", "B", "A", "B", "A", "A")
)

# Drop
df1 <- df[df$Name != "Augustine D. Salinas", ] df1 Output:  Name ID Math English Class 1 Marnie W. Vivanco 1 8 8 A 3 Amanda R. Goldstein 3 6 10 A 4 Clarence E. Almodovar 4 10 8 B 5 Alan T. Feuerstein 5 8 9 A 6 Emma J. Lawrence 6 7 9 A ### Drop rows or columns with the subset() function In the example below, we will use the subset() function to drop the rows. # Create a data frame df <- data.frame( Name = c( "Marnie W. Vivanco", "Augustine D. Salinas", "Amanda R. Goldstein", "Clarence E. Almodovar", "Alan T. Feuerstein", "Emma J. Lawrence" ), ID = c(1:6), Math = c(8, 9, 6, 10, 8, 7), English = c(8, 10, 10, 8, 9, 9), Class = c("A", "B", "A", "B", "A", "A") ) # Drop df1 <- subset(df, Name != "Augustine D. Salinas" & Name != "Alan T. Feuerstein") df1 Output:  Name ID Math English Class 1 Marnie W. Vivanco 1 8 8 A 3 Amanda R. Goldstein 3 6 10 A 4 Clarence E. Almodovar 4 10 8 B 6 Emma J. Lawrence 6 7 9 A For another example, we will drop the columns with the subset() function as the following: # Create a data frame df <- data.frame( Name = c( "Marnie W. Vivanco", "Augustine D. Salinas", "Amanda R. Goldstein", "Clarence E. Almodovar", "Alan T. Feuerstein", "Emma J. Lawrence" ), ID = c(1:6), Math = c(8, 9, 6, 10, 8, 7), English = c(8, 10, 10, 8, 9, 9), Class = c("A", "B", "A", "B", "A", "A") ) # Drop df1 <- subset(df, select = -c(Math, English)) df1 Output:  Name ID Class 1 Marnie W. Vivanco 1 A 2 Augustine D. Salinas 2 B 3 Amanda R. Goldstein 3 A 4 Clarence E. Almodovar 4 B 5 Alan T. Feuerstein 5 A 6 Emma J. Lawrence 6 A Besides, we can drop the columns by index. See the code example: # Create a data frame df <- data.frame( Name = c( "Marnie W. Vivanco", "Augustine D. Salinas", "Amanda R. Goldstein", "Clarence E. Almodovar", "Alan T. Feuerstein", "Emma J. Lawrence" ), ID = c(1:6), Math = c(8, 9, 6, 10, 8, 7), English = c(8, 10, 10, 8, 9, 9), Class = c("A", "B", "A", "B", "A", "A") ) # Drop df1 <- subset(df, select = -c(3, 5)) df1 Output:  Name ID English 1 Marnie W. Vivanco 1 8 2 Augustine D. Salinas 2 10 3 Amanda R. Goldstein 3 10 4 Clarence E. Almodovar 4 8 5 Alan T. Feuerstein 5 9 6 Emma J. Lawrence 6 9 ### Drop rows or columns with missing value Here, we will create a data frame with a missing value. # Create a data frame df <- data.frame( Name = c( "Marnie W. Vivanco", "Augustine D. Salinas", "Amanda R. Goldstein", "Clarence E. Almodovar", "Alan T. Feuerstein", "Emma J. Lawrence" ), ID = c(1:6), Math = c(8, 9, NA, 10, 8, NA), English = c(8, 10, 10, 8, NA, 9), Class = c("A", "B", "A", "B", "A", "A") ) # View a data frame df Output:  Name ID Math English Class 1 Marnie W. Vivanco 1 8 8 A 2 Augustine D. Salinas 2 9 10 B 3 Amanda R. Goldstein 3 NA 10 A 4 Clarence E. Almodovar 4 10 8 B 5 Alan T. Feuerstein 5 8 NA A 6 Emma J. Lawrence 6 NA 9 A Now, we will drop the rows which have missing values in this data frame. # Create a data frame df <- data.frame(Name=c("Marnie W. Vivanco", "Augustine D. Salinas", "Amanda R. Goldstein", "Clarence E. Almodovar", "Alan T. Feuerstein", "Emma J. Lawrence"), ID=c(1:6), Math=c(8, 9, NA, 10, 8, NA), English=c(8, 10, 10, 8, NA, 9), Class=c("A", "B", "A", "B", "A", "A")) # Drop the rows have missing value df1 <- df[!(is.na(df$Math) | is.na(df\$English)),]
df1

Output:

                  Name ID Math English Class
1     Marnie W. Vivanco  1    8       8     A
2  Augustine D. Salinas  2    9      10     B
4 Clarence E. Almodovar  4   10       8     B

## Summary

This tutorial will demonstrate how to drop the rows or columns in the R programming language. If you have any questions, please leave a comment below.

Have a great day!

Maybe you are interested:

Posted in R