Table of Contents
Dropping columns from a data frame in R refers to removing specific columns of data from a larger dataset. This can be achieved by using the “subset” function and specifying which columns to drop. For example, if we have a data frame called “df” with columns labeled “A”, “B”, and “C”, we can drop the “C” column by using the code “df <- subset(df, select = -C)”. This will create a new data frame without the “C” column. Other methods for dropping columns include using the “select” function from the “dplyr” package or indexing the columns to be dropped using negative numbers. Ultimately, dropping columns allows for more efficient and focused analysis on a specific subset of data within a larger dataset.
Drop Columns from Data Frame in R (With Examples)
The easiest way to drop columns from a data frame in R is to use the subset() function, which uses the following basic syntax:
#remove columns var1 and var3 new_df <- subset(df, select = -c(var1, var3))
The following examples show how to use this function in practice with the following data frame:
#create data frame df <- data.frame(var1=c(1, 3, 3, 4, 5), var2=c(7, 7, 8, 3, 2), var3=c(3, 3, 6, 10, 12), var4=c(14, 16, 22, 19, 18)) #view data frame df var1 var2 var3 var4 1 1 7 3 14 2 3 7 3 16 3 3 8 6 22 4 4 3 10 19 5 5 2 12 18
Example 1: Drop Columns by Name
The following code shows how to drop columns from the data frame by name:
#remove columns var1 and var3 new_df <- subset(df, select = -c(var1, var3)) #view updated data frame new_df var2 var4 1 7 14 2 7 16 3 8 22 4 3 19 5 2 18
Example 2: Drop Columns by Index
The following code shows how to drop columns from the data frame by index:
#remove first and fourth columns new_df <- subset(df, select = -c(1, 4)) #view updated data frame new_df var2 var3 1 7 3 2 7 3 3 8 6 4 3 10 5 2 12
Example 3: Drop Columns in List
The following code shows how to drop columns from the data frame that belong to a certain list:
#define list of columns to remove remove_cols <- c('var1', 'var4') #remove columns in list new_df = subset(df, select = !(names(df) %in% remove_cols)) #view updated data frame new_df var2 var3 1 7 3 2 7 3 3 8 6 4 3 10 5 2 12
Example 4: Drop Columns in Range
The following code shows how to drop columns from the data frame in a certain range:
#remove columns in range of 1 to 3 new_df = subset(df, select = -c(1:3)) #view updated data frame new_df var4 1 14 2 16 3 22 4 19 5 18
Cite this article
stats writer (2024). How can we drop columns from a data frame in R? Can you provide some examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-we-drop-columns-from-a-data-frame-in-r-can-you-provide-some-examples/
stats writer. "How can we drop columns from a data frame in R? Can you provide some examples?." PSYCHOLOGICAL SCALES, 1 May. 2024, https://scales.arabpsychology.com/stats/how-can-we-drop-columns-from-a-data-frame-in-r-can-you-provide-some-examples/.
stats writer. "How can we drop columns from a data frame in R? Can you provide some examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-we-drop-columns-from-a-data-frame-in-r-can-you-provide-some-examples/.
stats writer (2024) 'How can we drop columns from a data frame in R? Can you provide some examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-we-drop-columns-from-a-data-frame-in-r-can-you-provide-some-examples/.
[1] stats writer, "How can we drop columns from a data frame in R? Can you provide some examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can we drop columns from a data frame in R? Can you provide some examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
