How to drop data frame columns by name

0 votes

This is a sample data frame:

df1<-as.data.frame(matrix(sample(c( 1:10), 100, replace = TRUE), 10))
> df1
   V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
1  10  6  6  2  1  7  5  9  6   1
2   3  6 10  2  9  8  5  8  8   4
3  10  6  6 10  5  7  3  3 10   1
4   8  5  1 10  1 10  9 10  3   4
5   8  8  7  6  2  3  2 10  6   5
6   9  3  3  6  8  1  3  8  7  10
7   5  5  8 10 10  2  5  6  9   3
8   7  9  1  9  7  7  4  8  1   9
9   9 10 10  5  5  6 10  1  8   5
10  6 10  5  7  5  6  6  3  8  10

I want to remove v2 and v4 columns, but how?

Thanks in Advance!!

Apr 10, 2018 in Data Analytics by CodingByHeart77
• 3,750 points

edited Apr 10, 2018 by CodingByHeart77 831 views

1 answer to this question.

0 votes

Easily you can do it:

drops <- c("V2","V4")
df1[ , !(names(df1) %in% drops)]
df1[ , !(names(df1) %in% drops)]
   V1 V3 V5 V6 V7 V8 V9 V10
1  10  6  1  7  5  9  6   1
2   3 10  9  8  5  8  8   4
3  10  6  5  7  3  3 10   1
4   8  1  1 10  9 10  3   4
5   8  7  2  3  2 10  6   5
6   9  3  8  1  3  8  7  10
7   5  8 10  2  5  6  9   3
8   7  1  7  7  4  8  1   9
9   9 10  5  6 10  1  8   5
10  6  5  5  6  6  3  8  10
answered Apr 10, 2018 by DeepCoder786
• 1,720 points

Related Questions In Data Analytics

+1 vote
2 answers

How can I drop columns by name in a data frame ?

We can Drop Columns by name in ...READ MORE

answered Apr 14, 2018 in Data Analytics by zombie
• 3,790 points
28,428 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,404 views
0 votes
2 answers

In data frame how to spilt strings into values?

You can do this using dplyr and ...READ MORE

answered Dec 5, 2018 in Data Analytics by Kalgi
• 52,350 points
1,099 views
0 votes
1 answer

How to convert a text mining termDocumentMatrix into excel or csv in R?

By assuming that all the values are ...READ MORE

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

How to create a list of Data frames?

Basically all we have to do is ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,341 views
+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,813 views
0 votes
1 answer

How to replace NA values in a dataframe with Zero's ?

It is simple and easy: df1<-as.data.frame(matrix(sample(c(NA, 1:10), 100, ...READ MORE

answered Apr 10, 2018 in Data Analytics by CodingByHeart77
• 3,750 points
2,062 views
0 votes
1 answer

How to spilt a column of a data frame into multiple columns

it is easily achievable by using "stringr" ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,775 views
+1 vote
1 answer

How to convert a list of vectors with various length into a Data.Frame?

We can easily use this command as.data.frame(lapply(d1, "length< ...READ MORE

answered Apr 4, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,661 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