Remove values from a dataset based on a vector of those values

后端 未结 2 967
执念已碎
执念已碎 2021-01-24 20:01

I have a dataset that looks like this, except it\'s much longer and with many more values:

dataset <- data.frame(grps = c(\"a\",\"b\",\"c\",\"a\",\"d\",\"b\"         


        
2条回答
  •  隐瞒了意图╮
    2021-01-24 20:51

    There's also subset:

    subset(dataset, !(grps %in% remove))
    

    ... which is really just a wrapper around [ that lets you skip writing dataset$ over and over when there are multiple subset criteria. But, as the help page warns:

    This is a convenience function intended for use interactively. For programming it is better to use the standard subsetting functions like ‘[’, and in particular the non-standard evaluation of argument ‘subset’ can have unanticipated consequences.

    I've never had any problems, but the majority of my R code is scripting for my own use with relatively static inputs.


    2013-04-12

    I have now had problems. If you're building a package for CRAN, R CMD check will throw a NOTE if you have use subset in this way in your code - it will wonder if grps is a global variable, even though subset is evaluating it within dataset's environment (not the global one). So if there's any possiblity your code will end up in a package and you feel squeamish about NOTEs, stick with Rcoster's method.

提交回复
热议问题