Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+3 votes
7 views
in R Programming by (10.2k points)
edited by

I want to remove some questions from data frame, I know how to delete them individually using

df$x <- NULL

But I want to know a shortcut to perform this task.

I know how to drop columns using integer index using this command:

df <- df[ -c(1, 3:6, 12) ]

But I fear this might change the relative position of my variables.

So, Is there a better way than dropping each column one by one?

1 Answer

0 votes
by (46k points)
edited by

Subset Command can be useful if you know which column you want:

df <- data.frame(q = 2:11, e = 3:12, w = 4:13)

df <- subset(df, select = w(q, w))

Or you can also drop columns using:

df <- subset(df, select = -w(q, w))

Alternatively you can also use drop argument if you want to keep one column as a data frame, for this use:

keeps <- "V"

DF[ , keeps, drop = FALSE]

To drop unnecessary dimensions use drop=True and finally return a vector with the values of column V

Enjoy Learning. Cheers.

Related questions

+1 vote
1 answer
+4 votes
2 answers
asked May 29, 2019 in R Programming by Krishna (2.6k points)
+1 vote
1 answer
asked May 23, 2019 in R Programming by Nigam (4k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...