How can I drop columns by name in a data frame

+1 vote
Apr 13, 2018 in Data Analytics by shams
• 3,670 points
28,426 views

2 answers to this question.

+1 vote
We can Drop Columns by name in a Data frame using following two methods:

Create a data frame

The following code creates a sample data frame which we will use for demonstration.
set.seed(556)
mydata <- data.frame(m=letters[1:5], x=runif(5,10,50), y=sample(5), z=rnorm(5))
# Delete column by name

Method I :

One of the most easiest way to drop columns is by using subset() function.
The following code tells R to drop variables x and z.
The '-' sign indicates dropping variables.
Note: Don't specify the variable names in quotes when using subset() function.

df = subset(mydata, select = -c(x,z) )

> mydata
  m        x y           z
1 a 33.83910 2 -1.02569136
2 b 23.50851 3  0.59367584
3 c 22.91966 1 -0.06436851
4 d 33.18065 4 -0.04719129
5 e 13.79128 5 -1.80186137

> df
  m y
1 a 2
2 b 3
3 c 1
4 d 4
5 e 5

Method II :

The function names() returns all the column names and the '!' sign indicates negation.
drop <- c("x","z") #we are creating a character vector named drop in which we are storing column names x and z
df = mydata[,!(names(mydata) %in% drop)] #Selecting all the variables except the column names specified in the vector drop. The '!' sign indicates negation.
> df
  m y
1 a 2
2 b 3
3 c 1
4 d 4
5 e 5

It can also be written like :  df = mydata[,!(names(mydata) %in% c("x","z"))]

> set.seed(556)
> mydata <- data.frame(m=letters[1:5], x=runif(5,10,50), y=sample(5), z=rnorm(5))
> df = mydata[,!(names(mydata) %in% c("x","z"))]
> df
  m y
1 a 2
2 b 3
3 c 1
4 d 4
5 e 5
answered Apr 14, 2018 by zombie
• 3,790 points
0 votes

Hi,

The most easiest way to drop columns is by using subset() function. In the code below, we are telling R to drop variables x and z. The '-' sign indicates dropping variables.

$ mydata <- data.frame(a=letters[1:5], x=runif(5,10,50), y=sample(5), z=rnorm(5))
$ df = subset(mydata, select = -c(x,z) )
answered Dec 11, 2020 by MD
• 95,460 points

Related Questions In Data Analytics

+1 vote
2 answers

How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
1,812 views
0 votes
1 answer

How to drop data frame columns by name?

Easily you can do it: drops <- c("V2","V4") df1[ ...READ MORE

answered Apr 10, 2018 in Data Analytics by DeepCoder786
• 1,720 points
830 views
0 votes
1 answer
0 votes
1 answer

How to drop factor levels in a subsetted data frame?

You can use factor(ff) to drop levels ...READ MORE

answered Apr 17, 2018 in Data Analytics by kappa3010
• 2,090 points

edited Apr 17, 2018 by kappa3010 5,401 views
+1 vote
2 answers

How can I get experience in Data Science as a fresher?

Work on projects of your own. It’s tough, ...READ MORE

answered Aug 9, 2018 in Data Analytics by Abhi
• 3,720 points
884 views
0 votes
1 answer

How can I find number of columns in a dataframe

To know the number of columns in ...READ MORE

answered Oct 12, 2018 in Data Analytics by Abhi
• 3,720 points
602 views
0 votes
1 answer

Shiny r ,I'm doing a dashboard and I can not replace in the table below the name of the column by choosing the selectinput.

Hi, When you want to change any input ...READ MORE

answered Aug 19, 2019 in Data Analytics by anonymous
• 33,030 points
1,151 views
0 votes
1 answer

How can I calculate mean per group in a data.frame?

You can use aggregate function for calculating ...READ MORE

answered May 24, 2018 in Data Analytics by zombie
• 3,790 points
3,954 views
0 votes
1 answer

How to convert tables to a data frame in R ?

> trial.table.df <- as.data.frame(trial.table) //assuming that trial.table ...READ MORE

answered Apr 20, 2018 in Data Analytics by zombie
• 3,790 points
7,601 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP