0

First of all, I have to say that this is my first post. Despite of having look for the answer using the search toolbox it might be possible that I passed over the right topic without realizing myself, so just in case sorry for that.

Having said that, my problem is the following one:

  • I have a data table composed by several columns.
  • I have to select the rows that are fullfilling one specific condition ex. which(DT_$var>value, arr.ind = T)) or which(DT_$var>value && DT_$var2>value2, arr.ind = T))
  • I have to keep these columns in a new data frame.

My approach was the following one but it is not working, probably because I did not understand the loops correctly:

while (i in nrow(DT)) {
    if(DT$var[i]>value){
        DT_aux[i]=DT[i]
        i<-i+1
    }

}
Error in if (DT$value[i] > 45) { : argument is of length zero

I hope that you can help me

tfkLSTM
  • 161
  • 13
  • 1
    Welcome to SO! Please make your example [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – jogo Mar 20 '18 at 08:05

1 Answers1

2

There is a very good chance that you want to use dplyr and it's filter function. It would work like this:

library(dplyr)
DT %>% filter(var>value && var2>value2)

You don't need to use DT$var and DT$var2 here; dplyr knows what you mean when you refer to variables.

You can, of course, do the same with base R, but this kind of work is exactly what dplyr was made for, so sticking with base R, in this case, is just masochism.

Thomas Mailund
  • 1,674
  • 10
  • 16
  • Dear Thomas, Your solution worked like a charm, however for some reason that I do not understand I have to specify the logical operator as & instead of &&. – tfkLSTM Mar 20 '18 at 11:17
  • Ah, sorry. The filter function considers the columns as vectors, I guess, so you have to use the vector version of the logical operators. I usually just separate the conditions with commas, though, as that works as logical and. – Thomas Mailund Mar 20 '18 at 11:38