2013-02-02 9 views
5

Ho un frame di dati denominato ycreando un vettore logica dal frame di dati

dput(y) 
structure(list(val1 = c(25L, 615L, 30L, 76L, 97L, 211L, 0L, 40L, 
10L, 10L), val2 = c(101L, 286L, 124L, 77L, 176L, 120L, 0L, 8L, 
56L, 49L), val3 = c(157L, 454L, 106L, 242L, 144L, 31L, 0L, 40L, 
45L, 57L)), .Names = c("val1", "val2", "val3"), row.names = c(NA, 
10L), class = "data.frame") 

Vorrei poter guardare val1 e val2 colonne e se val1> 10, sostituire il valore con TRUE altrimenti FALSE e guarda val2 e se val2 < 5, sostituisci il valore con TRUE else FALSE

Posso usare la funzione sottoinsieme per selezionarli ma piuttosto che usare sottoinsieme, mi piace sostituire i valori con VERO o FALSO, qualsiasi idea come vorrei Fai questo?

risposta

12

Basta utilizzare gli operatori booleani:

y$val1 > 10 | (y$val1 <=10 & y$val2<5) 
# [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE 
3
transform(dat, x= val1 > 10 , y = val2 < 5) 
    val1 val2 val3  x  y 
1 25 101 157 TRUE FALSE 
2 615 286 454 TRUE FALSE 
3 30 124 106 TRUE FALSE 
4 76 77 242 TRUE FALSE 
5 97 176 144 TRUE FALSE 
6 211 120 31 TRUE FALSE 
7  0 0 0 FALSE TRUE 
8 40 8 40 TRUE FALSE 
9 10 56 45 FALSE FALSE 
10 10 49 57 FALSE FALSE 
Problemi correlati