2

I am trying to figure out how to remove a group of rows from a dataset by reference. For example, with this data set:

testset <- data.table(date=as.Date(c("2013-07-02","2013-08-03","2013-09-04","2013-10-05","2013-11-06")), 
           yr = c(2013,2013,2013,2013,2013), 
           mo = c(07,08,09,10,11), 
           da = c(02,03,04,05,06), 
           plant = LETTERS[1:5], 
           product = as.factor(letters[26:22]), 
           rating = runif(25))

I want to remove all rows where the product is "y". I have no idea how to go about this.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
black_sheep07
  • 2,308
  • 3
  • 26
  • 40

1 Answers1

-4

you can use either of the following commands -

testset_new <- subset(testset,product!="y")

or

testset_new <- testset[testset$product!="y",]
RHelp
  • 815
  • 2
  • 8
  • 23
  • 6
    This doesn't answer the question since it copies the data.table. – Roland Apr 11 '14 at 12:58
  • In that case, you overwrite your existing data - testset <- subset(testset,product!="y") – RHelp Apr 15 '14 at 09:47
  • @RHelp That's not the point. What if you are inside the function and you are expected to modify the data.table given in the arguments? Replacing **is not** modification in place (by reference). – Adam Ryczkowski Jul 01 '16 at 22:39