2

I have a usual 100 by 2 database.

I want to keep only values where 1st column ('station') equals 3 and 2nd column ('complete') equals 1.

However none of my tries works out:

data[data[,2]==1]
subset(data,complete=1)
data[complete=1] - returns just 1
data[data$complete ==1] -returns an error: $ operator is invalid for atomic vectors
Ilja
  • 611
  • 3
  • 9
  • 19
  • 1
    Try `subset(data, station==3 & complete==1)` or using `[` , `data[data$station==3 & data$complete==1,]` In your `subset`, the `=` should be `==`. In the third line, also the same problem along with absence of a `,` after 1. The error suggests that you may not have a `data.frame`. Please consider providing some example data using `dput` – akrun Aug 11 '15 at 13:58
  • It does not work, what I don't understand. It says, object 'station' not found. On the data viewer however I see both column names. I tried: data[data[,2] == 1 & data[,2] ==1], but this returned only a long vector with all the possible values from one to 3. – Ilja Aug 11 '15 at 14:04
  • 3
    That is what I mentioned to provide a `reproducible example` using dput. If your data is a matrix, `$` won't work. In that case try `data[data[,'station']==3 & data[,'complete']==1,]` – akrun Aug 11 '15 at 14:05
  • Your code is incorrect in two ways, 1) you are repeating `data[,2]==1` and 2), it should be a row index. So, after the 1, there should be a `,` – akrun Aug 11 '15 at 14:07
  • Thank you. It was the issue with data.frame. I used cbind instead of data.frame. Now your first suggestion worked out. – Ilja Aug 11 '15 at 14:12
  • 2
    As I mentioned, `$` wont' work with `matrix`. so, you can either use numeric index or if you have column names, quote it to subset the columns – akrun Aug 11 '15 at 14:14
  • with `dplyr` you can use this code: `df %>% filter(station == 3 & complete == 1)`. – SabDeM Aug 11 '15 at 18:26

0 Answers0