[R] conditional with and operators
Christos Hatzis
christos.hatzis at nuverabio.com
Tue Aug 19 22:45:52 CEST 2008
Mark,
It is not clear whether you are dealing with a single list or something more
complex, such as a list of lists or a list of data frames. In the case of a
single list, you don't really need any of the 'apply' functions. The main
problem in your code is the use of '&&' instead of '&':
> test.1 <- list(id = 1:50, tick = rnorm(50, 900, 50), tvis = sample(1:100,
50))
> test.1
$id
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
25 26 27 28 29
[30] 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
$tick
[1] 891.5450 997.4262 884.0224 892.8317 903.7194 862.7283 913.8126
895.6072
[9] 923.2599 823.0804 828.0575 919.2257 879.8593 833.6082 924.6253
889.6797
[17] 880.2397 1022.9587 898.3806 953.0213 911.8132 932.3018 960.7257
905.0871
[25] 1018.8407 991.3910 931.3846 960.0569 793.5923 899.3268 921.0783
929.6885
[33] 940.9550 882.9791 914.9050 897.7252 970.7575 867.9848 901.2766
912.0851
[41] 856.8671 878.3230 906.9869 903.8044 882.1902 972.3030 923.9107
869.6903
[49] 836.6934 912.6101
$tvis
[1] 67 8 89 36 91 30 63 38 22 44 18 79 48 90 82 6 81 39 69 7 45 86 32 43
99 23 34 54 37
[30] 1 15 5 14 87 77 27 93 94 71 60 83 62 92 72 26 80 58 98 84 78
> test.1$id > 10 & test.1$tick > 910
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
FALSE FALSE
[15] TRUE FALSE FALSE TRUE FALSE TRUE TRUE TRUE TRUE FALSE TRUE TRUE
TRUE TRUE
[29] FALSE FALSE TRUE TRUE TRUE FALSE TRUE FALSE TRUE FALSE FALSE TRUE
FALSE FALSE
[43] FALSE FALSE FALSE TRUE TRUE FALSE FALSE TRUE
> test.1$tvis[test.1$id > 10 & test.1$tick > 910]
[1] 79 82 39 7 45 86 32 99 23 34 54 15 5 14 77 93 60 80 58 78
If this is not what you have, it would be helpful to provide an example of
the data structure that you are dealing with, as the posting guide suggests.
-Christos
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Altaweel, Mark R.
> Sent: Tuesday, August 19, 2008 4:10 PM
> To: r-help at r-project.org
> Subject: [R] conditional with and operators
>
> Hi,
>
> I have a problem in which I am parsing data from a list. I
> am simply trying to return data that has several conditions
> being true. Here is my syntax below:
>
> d<-sapply(res,function(.df){(.df$TimesVisited[.df$Tick>912 &&
> .df$Id>0])}) #res is the list, and I am trying to return a
> result that has two true conditions (that is the variable
> Tick should be greate than 912 and Id should be greater than 0
>
> This returns an array of 10 with integer values of 0. This is
> the incorrect result
>
> However, if I do the same syntax except I remove the &&
> statement (i.e. the second conditional), then the result
> producing something that makes sense, which is all values
> that are Tick and greater than 912.
>
> Can someone let me know how I can setup my data to be parsed
> so I can have 2 or multiple conditionals in my function that
> is looking at an array.
>
> Thanks in advance.
>
> Mark
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
More information about the R-help
mailing list