R will return a vector with all the values contained in that variable. Note again that the row names are dropped here. With this dollar-sign method, you can access only one variable at a time. If you want to access multiple variables at once using their names, you need to use the square brackets. I want to know If I want to delete this variable from the data set how I can perform this on R. Himansu979 February 5, 2016, 8:06pm #2 set the variable to NULL to drop it.
You should use either indexing or the subset function. For example: R df dfx y z u1 1 2 3 42 2 3 4 53 3 4 5 64 4 5 6 75 5 6 7 8Then you can use the which function and the - operator in column indexation: R df, -which(names(df)%in% c('z','u'))x y1 1 22 2 33 3 44 4 55 5 6Or, much simpler, use the select argument of the subset function: you can then use the - operator directly on a vector of column names, and you can even omit the quotes around the names!
![R drop variables if R drop variables if](https://cdn-images-1.medium.com/max/1600/1*gV0MRWkHPD5TZCjWIu8RvQ.png)
R subset(df, select=-c(z,u))x y1 1 22 2 33 3 44 4 55 5 6Note that you can also select the columns you want instead of dropping the others: R df, c('x','y')x y1 1 22 2 33 3 44 4 55 5 6R subset(df, select=c(x,y))x y1 1 22 2 33 3 44 4 55 5 6. Do not use -which for this, it is extremely dangerous. Consider: dat.
Here's a quick solution for this. Say, you have a data frame X with three columns A, B and C: X XA B C1 1 3 52 2 4 6If I want to remove a column, say B, just use grep on colnames to get the column index, which you can then use to omit the column.
X XA C1 1 52 2 6The beauty of grep is that you can specify multiple columns that match the regular expression. If I had X with five columns (A,B,C,D,E): X XA B C D E1 1 3 5 7 92 2 4 6 8 10Take out columns B and D: X XA C E1 1 5 92 2 6 10EDIT: Considering the grepl suggestion of Matthew Lundberg in the comments below: X XA B C D E1 1 3 5 7 92 2 4 6 8 10 X XA C E1 1 5 92 2 6 10If I try to drop a column that's non-existent,nothing should happen: X XA C E1 1 5 92 2 6 10. I tried to delete a column while using the package data.table and got an unexpected result. I kind of think the following might be worth posting. Just a little cautionary note. Edited by Matthew. DF = read.table(text = 'fruit state grade y1980 y1990 y2000apples Ohio aa 500 100 55apples Ohio bb 0 0 44apples Ohio cc 700 0 33apples Ohio dd 300 50 66', sep = ', header = TRUE, stringsAsFactors = FALSE)DF,!names(DF)%in% c('grade') # all columns other than 'grade'fruit state y1980 y1990 y20001 apples Ohio 500 100 552 apples Ohio 0 0 443 apples Ohio 700 0 334 apples Ohio 300 50 66library('data.table')DT = as.data.table(DF)DT,!names(dat4)%in% c('grade') # not expected!!
Not the same as DF!!1 TRUE TRUE FALSE TRUE TRUE TRUEDT,!names(DT)%in% c('grade'), with=FALSE # that's betterfruit state y1980 y1990 y20001: apples Ohio 500 100 552: apples Ohio 0 0 443: apples Ohio 700 0 334: apples Ohio 300 50 66Basically, the syntax for data.table is NOT exactly the same as data.frame. There are in fact lots of differences, see FAQ 1.1 and FAQ 2.17. You have been warned! Here is another solution that may be helpful to others. The code below selects a small number of rows and columns from a large data set.
Rules for selectionArguments for selecting columns are passed toand are treated specially. Unlike otherverbs, selecting functions make a strict distinction between dataexpressions and context expressions.A data expression is either a bare name like x or an expressionlike x:y or c(x, y).
In a data expression, you can only referto columns from the data frame.Everything else is a context expression in which you can onlyrefer to objects that you have defined with.