# Issue

This Content is from Stack Overflow. Question asked by gcz day

I read the post from How to drop columns by name in a data frame.
However, when I followed the step of

``````dat = dat[,!names(dat) %in% c("New York")]
``````

I got this output:

`````` [1]  TRUE  TRUE  FALSE

``````

but I want this output:
| Date | London | Paris |
| ——– | ————– |————– |
| 01-01-2000 | 1 | 3 |
| 02-01-2000 | 2 | 4 |

The dataset is as follows:
| Date | London | Paris | New York |
| ——– | ————– |————– |————– |
| 01-01-2000 | 1 | 3 | 8 |
| 02-01-2000 | 2 | 4 | 9 |

# Solution

You should use either indexing or the `subset` function. For example :

``````R> df <- data.frame(x=1:5, y=2:6, z=3:7, u=4:8)
R> df
x y z u
1 1 2 3 4
2 2 3 4 5
3 3 4 5 6
4 4 5 6 7
5 5 6 7 8
``````

Then you can use the `which` function and the `-` operator in column indexation :

``````R> df[ , -which(names(df) %in% c("z","u"))]
x y
1 1 2
2 2 3
3 3 4
4 4 5
5 5 6
``````

Or, 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> subset(df, select=-c(z,u))
x y
1 1 2
2 2 3
3 3 4
4 4 5
5 5 6
``````

Note that you can also select the columns you want instead of dropping the others :

``````R> df[ , c("x","y")]
x y
1 1 2
2 2 3
3 3 4
4 4 5
5 5 6

R> subset(df, select=c(x,y))
x y
1 1 2
2 2 3
3 3 4
4 4 5
5 5 6
``````

``` This Question was asked in  StackOverflow by  leroux and Answered by juba It is licensed under the terms of
CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.```