Back to Browse

Remove Data Frame Columns by Name in R (6 Examples) | Drop Variable | subset, within, select & setDT

10.7K views
Jan 4, 2021
8:06

How to drop the variable of a data frame by its name in the R programming language. More details: https://statisticsglobe.com/r-remove-data-frame-columns-by-name R code of this video: data <- data.frame(x1 = 1:5, # Create example data x2 = 6:10, x3 = letters[1:5], x4 = letters[6:10]) data1 <- data[ , ! names(data) %in% c("x1", "x3")] # Apply %in%-operator data2 <- data[ , names(data) %in% c("x2", "x4")] # Keep certain variables data3 <- subset(data, select = - c(x1, x3)) # Apply subset function data4 <- within(data, rm(x1, x3)) # Apply within function install.packages("dplyr") # Install dplyr package library("dplyr") # Load dplyr data5 <- select(data, - c(x1, x3)) # Apply select function install.packages("data.table") # Install & load data.table package library("data.table") data6 <- data setDT(data6)[ , c("x1", "x3") := NULL] # Using := NULL class(data6) # Check class of data Follow me on Social Media: Facebook: https://www.facebook.com/statisticsglobecom/ Patreon: https://www.patreon.com/statisticsglobe Pinterest: https://www.pinterest.de/JoachimSchork Reddit: https://www.reddit.com/user/JoachimSchork Twitter: https://twitter.com/JoachimSchork

Download

0 formats

No download links available.

Remove Data Frame Columns by Name in R (6 Examples) | Drop Variable | subset, within, select & setDT | NatokHD