Altaweel, Mark R. | 19 Aug 22:07
Favicon

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.

Steven McKinney | 19 Aug 22:33
Favicon

Re: conditional with and operators


Did you try it with the vector '&' and operator?

d<-sapply(res,function(.df){(.df$TimesVisited[.df$Tick>912 & .df$Id>0])}) 

(The '&&' operator is designed for use in
e.g. if() clauses where you want a scalar logical
answer)

HTH

Steve McKinney

-----Original Message-----
From: r-help-bounces <at> r-project.org on behalf of Altaweel, Mark R.
Sent: Tue 8/19/2008 1: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

(Continue reading)

Altaweel, Mark R. | 19 Aug 22:39
Favicon

Re: conditional with and operators

Hi,

Ok that worked...im from a Java background so I guess Im used to the && rather than &

Thanks!

Mark

-----Original Message-----
From: Steven McKinney [mailto:smckinney <at> bccrc.ca]
Sent: Tue 8/19/2008 3:33 PM
To: Altaweel, Mark R.; r-help <at> r-project.org
Subject: RE: [R] conditional with and operators

Did you try it with the vector '&' and operator?

d<-sapply(res,function(.df){(.df$TimesVisited[.df$Tick>912 & .df$Id>0])}) 

(The '&&' operator is designed for use in
e.g. if() clauses where you want a scalar logical
answer)

HTH

Steve McKinney

-----Original Message-----
From: r-help-bounces <at> r-project.org on behalf of Altaweel, Mark R.
Sent: Tue 8/19/2008 1:10 PM
To: r-help <at> r-project.org
(Continue reading)

Christos Hatzis | 19 Aug 22:45
Favicon

Re: conditional with and operators

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

(Continue reading)


Gmane