-1

I'm working with a data frame inside a function,and I have a problem in the syntaxis. This is the frame I'm working with:

c
   id nobs
1   1  117
2   2 1041
3   3  243
4   4  474
5   5  402
6   6  228
7   7  442
8   8  192
9   9  275
10 10  148

And this is the code I'm using.

threshold=250
c
for (i in c[1]){ 
    if(threshold > any(c[i,2])){
        print(c[i,1])
    }
}

What I want is to get the first element of the data frame is the condition is met, but I get the result: [1] 1 2 3 4 5 6 7 8 9 10. It only has to be: 1 3 6 8 10

Any help would be appreciated. Thanks in advance.

  • 1
    `c` is a function. It may be better to name objects with other names – akrun May 15 '15 at 13:41
  • possible duplicate of [Filtering a data frame in R](http://stackoverflow.com/questions/1686569/filtering-a-data-frame-in-r) – Frank May 15 '15 at 14:04

4 Answers4

1

Use which

c[which(c$nobs<250),1]
Shenglin Chen
  • 4,504
  • 11
  • 11
0
subset(c, nobs > threshold)
Quinn Weber
  • 927
  • 5
  • 10
0

Not sure:

#df is your dataframe

option1:

 with(df,ifelse(nobs<250,lag(id,1),NA))
     [1]  1 NA  3 NA NA  6 NA  8 NA 10

option 2:

df[df$nobs<250,"id"]
[1]  1  3  6  8 10
user227710
  • 3,164
  • 18
  • 35
0

Let's call your data.frame x to avoid the conflict of the function name c.

Following your syntax, it should be:

for (i in seq_len(nrow(x))){ 
    if(threshold > x[i,2]){
        print(x[i,1])
    }
}

Or simply,

x[x[,2] < threshold, 1]
xb.
  • 1,617
  • 11
  • 16
  • This one actually did exactly what I was looking for, thank you very much! Besides that, would you kindly explain me the difference between using" for (i in c[1]){" (mine) and using "for (i in seq_len(nrow(x))){" ? – hachondeoro May 15 '15 at 14:20
  • @Frank `i in x[1]` loops over the elements of the 1st column, while `i in seq_len(nrow(x))` loops over the index of the rows. If the 1st column is extactly the row indexes, as in your case, there is no difference. The major issue of the initial code is the use of `any`. `any` returns `TRUE` for any non-`FALSE`/`0`/`NULL (or other empty)` R object. Therefore `threshold > any(x[i,2])` is always `TRUE` in your example. – xb. May 15 '15 at 16:12