I have run into a general problem with subsetting of which the following is a simple example. Suppose that x <- c(5, NA, 7, 5, NA, 3) y <- c(1, 2, 3, 4, 5, 6) I want to extract the values of y for which x = 5, but y[x==5] yields> y[x==5][1] 1 NA 4 NA I find that y[!is.na(x) & x==5] yields the desired result:> y[!is.na(x) & x==5][1] 1 4 but I am wondering whether there is a general way to specify a logical condition such that it yields TRUE when the condition is true and FALSE when the condition is either false or the input data is NA? For example, is there a simpler way to produce the same results as y[!is.na(x) & x==5]? John -------------------------------------------------------------------- John Miyamoto, Dept. of Psychology, Box 351525 University of Washington, Seattle, WA 98195-1525 Phone 206-543-0805, Fax 206-685-3157, Email jmiyamot at u.washington.edu Homepage http://faculty.washington.edu/jmiyamot/ -------------------------------------------------------------------- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> x <- c(5, NA, 7, 5, NA, 3) > y <- c(1, 2, 3, 4, 5, 6) > y[x %in% 5][1] 1 4 I learned about this trick, if I remember correctly, from reading the notes on what's new in the upcoming R 1.5.1 -Don At 6:17 PM -0700 4/22/02, John Miyamoto wrote:>I have run into a general problem with subsetting of which the following >is a simple example. Suppose that > >x <- c(5, NA, 7, 5, NA, 3) >y <- c(1, 2, 3, 4, 5, 6) > >I want to extract the values of y for which x = 5, but y[x==5] yields > >> y[x==5] >[1] 1 NA 4 NA > >I find that y[!is.na(x) & x==5] yields the desired result: > >> y[!is.na(x) & x==5] >[1] 1 4 > >but I am wondering whether there is a general way to specify a logical >condition such that it yields TRUE when the condition is true and FALSE >when the condition is either false or the input data is NA? For example, >is there a simpler way to produce the same results as y[!is.na(x) & x==5]? > >John > >-------------------------------------------------------------------- >John Miyamoto, Dept. of Psychology, Box 351525 >University of Washington, Seattle, WA 98195-1525 >Phone 206-543-0805, Fax 206-685-3157, Email jmiyamot at u.washington.edu >Homepage http://faculty.washington.edu/jmiyamot/ >-------------------------------------------------------------------- > >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- >r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html >Send "info", "help", or "[un]subscribe" >(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch >_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hi, John, Often I use y[ which(x == 5) ] to bypass the NAs, and y[ -which(x == 5) ] includes the mismatches and NAs. In all cases I have tested it works on both R and Splus, which I think is worth the extra which(). Also I think "x %in% c(5)" or match(x, table=5) in R gives you what you wanted. Yuelin. ----- On Mon, 22 Apr 2002, John Miyamoto wrote: I have run into a general problem with subsetting of which the following is a simple example. Suppose that x <- c(5, NA, 7, 5, NA, 3) y <- c(1, 2, 3, 4, 5, 6) I want to extract the values of y for which x = 5, but y[x==5] yields> y[x==5][1] 1 NA 4 NA I find that y[!is.na(x) & x==5] yields the desired result:> y[!is.na(x) & x==5][1] 1 4 but I am wondering whether there is a general way to specify a logical condition such that it yields TRUE when the condition is true and FALSE when the condition is either false or the input data is NA? For example, is there a simpler way to produce the same results as y[!is.na(x) & x==5]? John -------------------------------------------------------------------- John Miyamoto, Dept. of Psychology, Box 351525 University of Washington, Seattle, WA 98195-1525 Phone 206-543-0805, Fax 206-685-3157, Email jmiyamot at u.washington.edu Homepage http://faculty.washington.edu/jmiyamot/ -------------------------------------------------------------------- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
ggrothendieck@yifan.net
2002-Apr-23 04:40 UTC
[R] Subsetting by a logical condition and NA's
subset(y,x==5) On 22 Apr 2002 at 18:17, John Miyamoto wrote:> I have run into a general problem with subsetting of which the following > is a simple example. Suppose that > > x <- c(5, NA, 7, 5, NA, 3) > y <- c(1, 2, 3, 4, 5, 6) > > I want to extract the values of y for which x = 5, but y[x==5] yields > > > y[x==5] > [1] 1 NA 4 NA > > I find that y[!is.na(x) & x==5] yields the desired result: > > > y[!is.na(x) & x==5] > [1] 1 4 > > but I am wondering whether there is a general way to specify a logical > condition such that it yields TRUE when the condition is true and FALSE > when the condition is either false or the input data is NA? For example, > is there a simpler way to produce the same results as y[!is.na(x) & x==5]? > > John > > -------------------------------------------------------------------- > John Miyamoto, Dept. of Psychology, Box 351525 > University of Washington, Seattle, WA 98195-1525 > Phone 206-543-0805, Fax 206-685-3157, Email jmiyamot at u.washington.edu > Homepage http://faculty.washington.edu/jmiyamot/ > -------------------------------------------------------------------- > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._