Need help for logical expression

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Need help for logical expression

roslinazairimah zakaria
Dear all,

I just have a simple problem here.

I generate a sample data as follows:
set.seed(123456)
r1 <- sample(1:100,100 ,replace=T)
r2 <- sample(1:100,100 ,replace=T)
r3 <- sample(1:100,100 ,replace=T)

R <- cbind(r1,r2,r3); head(R)
> R <- cbind(r1,r2,r3); head(R)
     r1 r2 r3
[1,] 80  4 20
[2,] 76 66 14
[3,] 40 32 87
[4,] 35 19 24
[5,] 37 63 12
[6,] 20 52 42
sum_r <- rowSums(R)
all_pct <- round(R/sum_r*100,0); head(all_pct)
> all_pct <- round(R/sum_r*100,0); head(all_pct)
     r1 r2 r3
[1,] 77  4 19
[2,] 49 42  9
[3,] 25 20 55
[4,] 45 24 31
[5,] 33 56 11
[6,] 18 46 37

I would like to count how many of all_pct data satisfy this condition as
follows:

dt_all <- ifelse(all_pct[,1] >= 45 & all_pct[,1] > all_pct[,2] &
all_pct[,1] > all_pct[,3], 1, 0)

Note that data of all_pct[,1] >= 45  and at the same time greater
than all_pct[,2] and all_pct[,3].

How do I count how many satisfy the conditions?

--
*Roslinazairimah Zakaria*
*Tel: +609-5492370; Fax. No.+609-5492766*

*Email: [hidden email] <[hidden email]>;
[hidden email] <[hidden email]>*
Faculty of Industrial Sciences & Technology
University Malaysia Pahang
Lebuhraya Tun Razak, 26300 Gambang, Pahang, Malaysia

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Need help for logical expression

Jeff Newmiller
See below

On Mon, 7 Jan 2019, roslinazairimah zakaria wrote:

> Dear all,
>
> I just have a simple problem here.
>
> I generate a sample data as follows:
> set.seed(123456)
> r1 <- sample(1:100,100 ,replace=T)
> r2 <- sample(1:100,100 ,replace=T)
> r3 <- sample(1:100,100 ,replace=T)
>
> R <- cbind(r1,r2,r3); head(R)
>> R <- cbind(r1,r2,r3); head(R)
>     r1 r2 r3
> [1,] 80  4 20
> [2,] 76 66 14
> [3,] 40 32 87
> [4,] 35 19 24
> [5,] 37 63 12
> [6,] 20 52 42
> sum_r <- rowSums(R)
> all_pct <- round(R/sum_r*100,0); head(all_pct)
>> all_pct <- round(R/sum_r*100,0); head(all_pct)
>     r1 r2 r3
> [1,] 77  4 19
> [2,] 49 42  9
> [3,] 25 20 55
> [4,] 45 24 31
> [5,] 33 56 11
> [6,] 18 46 37
>
> I would like to count how many of all_pct data satisfy this condition as
> follows:
>
> dt_all <- ifelse(all_pct[,1] >= 45 & all_pct[,1] > all_pct[,2] &
> all_pct[,1] > all_pct[,3], 1, 0)
>
> Note that data of all_pct[,1] >= 45  and at the same time greater
> than all_pct[,2] and all_pct[,3].
>
> How do I count how many satisfy the conditions?

sum( dt_all )

Did I misunderstand?

>
> --
> *Roslinazairimah Zakaria*
> *Tel: +609-5492370; Fax. No.+609-5492766*
>
> *Email: [hidden email] <[hidden email]>;
> [hidden email] <[hidden email]>*
> Faculty of Industrial Sciences & Technology
> University Malaysia Pahang
> Lebuhraya Tun Razak, 26300 Gambang, Pahang, Malaysia
>
> [[alternative HTML version deleted]]

HTML email will eventually impede others trying to understand your code...
the sooner you stop posting HTML the better.

>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.