Reputation: 1359
I currently have ndat, a dataframe containing items and their respective data which looks like this:
> glimpse(ndat)
Observations: 203
Variables:
$ i5 (dbl) 1, 1, 1, 1, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1...
$ i4 (dbl) 1, 1, 1, 1, 2, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1...
$ i3 (dbl) 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1...
$ i2 (dbl) 1, 1, 1, 1, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2...
$ i1 (dbl) 1, 1, 1, 1, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1...
I also have "newDV" and "newCV" which are character strings containing the item names of several of the items found in ndat, and look like:
> newCV
[1] "i3" "i2"
> newDV
[1] "i1"
What I am trying to do is get ndat to contain the items that are not contained in newCV or newDV so it ends up looking like:
> glimpse(ndat)
Observations: 203
Variables:
$ i5 (dbl) 1, 1, 1, 1, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1...
$ i4 (dbl) 1, 1, 1, 1, 2, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1...
I have tried the following:
ndat<-as.data.frame(ndat[!(ndat %in% c(newCV,newDV))])
But this appears to work only for character string to character string, and not using character strings to specify columns in data frames. Any thoughts? Thank you!
Upvotes: 0
Views: 753
Reputation: 92282
You should simply filter by the column names of ndat
instead of its values, e.g.
ndat[,!(names(ndat) %in% c(newCV, newDV))]
Upvotes: 2