if statement - Selecting elements from Data Frame which meet an if condition inside a For Loop R -
i'm working data frame inside function,and have problem in syntaxis. 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 code i'm using.
threshold=250 c (i in c[1]){ if(threshold > any(c[i,2])){ print(c[i,1]) } }
what want first element of data frame condition met, result: [1] 1 2 3 4 5 6 7 8 9 10. has be: 1 3 6 8 10
any appreciated. in advance.
let's call data.frame x
avoid conflict of function name c
.
following syntax, 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]
Comments
Post a Comment