Conditions in R (Help Post)

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

Conditions in R (Help Post)

Yeasmin Alea
Hello Team
I would like to add a new column (for example-Phase) from the below data
set based on the conditions
   YEAR   DAY      X     Y   Sig
 1  1981     9 -0.213 1.08   1.10
 2  1981    10  0.065 1.05   1.05
*Conditions*

D$Phase=sapply(D,function(a,b) {
     a <-D$X
     b<-D$Y
     if (a<0 && b<0 && b<a)
    {phase=1} else if (a<0 && b<0 && b>a)
    {phase=2} else if (a<0 && b>0 && b<a)
    {phase=7} else if (a<0 && b>0 && b>a)
    {phase=8} else if (a>0 && b<0 && b<a)
    {phase=3} else if (a>0 && b<0 && b>a)
    {phase=4} else if (a>0 && b>0 && b>a)
    {phase=6} else (a>0 && b>0 && b<a)
    {phase=5}
})

Can anyone help to fix the script to get a Phase column based on the
conditions. The table will be like the below
   YEAR   DAY      X     Y   Sig      Phase
 1  1981     9 -0.213 1.08   1.10   phase=7
 2  1981    10  0.065 1.05   1.05   phase=6

Many thanks
Alea

        [[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: Conditions in R (Help Post)

jholtman
Here is one way of doing it; I think the output you show is wrong:

library(tidyverse)
input <- read_delim(" YEAR   DAY      X     Y   Sig
  1981     9 -0.213 1.08   1.10
  1981    10  0.065 1.05   1.05", delim = ' ', trim_ws = TRUE)

input <- mutate(input,
  phase = case_when(X < 0 & Y < 0 & Y < X ~ 'phase=1',
                    X < 0 & Y > 0 & Y < X ~ 'phase=2',
                    X < 0 & Y > 0 & Y < X ~ 'phase=7',
                    X < 0 & Y > 0 & Y > X ~ 'phase=8',
                    X > 0 & Y < 0 & Y < X ~ 'phase=3',
                    X > 0 & Y < 0 & Y > X ~ 'phase=4',
                    X > 0 & Y > 0 & Y > X ~ 'phase=6',
                    X > 0 & Y > 0 & Y < X ~ 'phase=5',
                    TRUE ~ 'unknown'
  )
)

> input
# A tibble: 2 x 6
   YEAR   DAY      X     Y   Sig phase
  <dbl> <dbl>  <dbl> <dbl> <dbl> <chr>
1  1981     9 -0.213  1.08  1.1  phase=8
2  1981    10  0.065  1.05  1.05 phase=6

Jim Holtman
*Data Munger Guru*


*What is the problem that you are trying to solve?Tell me what you want to
do, not how you want to do it.*


On Tue, Oct 22, 2019 at 9:43 AM Yeasmin Alea <[hidden email]> wrote:

> Hello Team
> I would like to add a new column (for example-Phase) from the below data
> set based on the conditions
>    YEAR   DAY      X     Y   Sig
>  1  1981     9 -0.213 1.08   1.10
>  2  1981    10  0.065 1.05   1.05
> *Conditions*
>
> D$Phase=sapply(D,function(a,b) {
>      a <-D$X
>      b<-D$Y
>      if (a<0 && b<0 && b<a)
>     {phase=1} else if (a<0 && b<0 && b>a)
>     {phase=2} else if (a<0 && b>0 && b<a)
>     {phase=7} else if (a<0 && b>0 && b>a)
>     {phase=8} else if (a>0 && b<0 && b<a)
>     {phase=3} else if (a>0 && b<0 && b>a)
>     {phase=4} else if (a>0 && b>0 && b>a)
>     {phase=6} else (a>0 && b>0 && b<a)
>     {phase=5}
> })
>
> Can anyone help to fix the script to get a Phase column based on the
> conditions. The table will be like the below
>    YEAR   DAY      X     Y   Sig      Phase
>  1  1981     9 -0.213 1.08   1.10   phase=7
>  2  1981    10  0.065 1.05   1.05   phase=6
>
> Many thanks
> Alea
>
>         [[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.
>

        [[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: Conditions in R (Help Post)

jholtman
Had the condition for phase=2 incorrect:

library(tidyverse)
input <- read_delim(" YEAR   DAY      X     Y   Sig
  1981     9 -0.213 1.08   1.10
  1981    10  0.065 1.05   1.05", delim = ' ', trim_ws = TRUE)

input <- mutate(input,
  phase = case_when(X < 0 & Y < 0 & Y < X ~ 'phase=1',
                    X < 0 & Y < 0 & Y > X ~ 'phase=2',
                    X < 0 & Y > 0 & Y < X ~ 'phase=7',
                    X < 0 & Y > 0 & Y > X ~ 'phase=8',
                    X > 0 & Y < 0 & Y < X ~ 'phase=3',
                    X > 0 & Y < 0 & Y > X ~ 'phase=4',
                    X > 0 & Y > 0 & Y > X ~ 'phase=6',
                    X > 0 & Y > 0 & Y < X ~ 'phase=5',
                    TRUE ~ 'unknown'
  )
)

Jim Holtman
*Data Munger Guru*


*What is the problem that you are trying to solve?Tell me what you want to
do, not how you want to do it.*


On Tue, Oct 22, 2019 at 11:20 AM jim holtman <[hidden email]> wrote:

> Here is one way of doing it; I think the output you show is wrong:
>
> library(tidyverse)
> input <- read_delim(" YEAR   DAY      X     Y   Sig
>   1981     9 -0.213 1.08   1.10
>   1981    10  0.065 1.05   1.05", delim = ' ', trim_ws = TRUE)
>
> input <- mutate(input,
>   phase = case_when(X < 0 & Y < 0 & Y < X ~ 'phase=1',
>                     X < 0 & Y > 0 & Y < X ~ 'phase=2',
>                     X < 0 & Y > 0 & Y < X ~ 'phase=7',
>                     X < 0 & Y > 0 & Y > X ~ 'phase=8',
>                     X > 0 & Y < 0 & Y < X ~ 'phase=3',
>                     X > 0 & Y < 0 & Y > X ~ 'phase=4',
>                     X > 0 & Y > 0 & Y > X ~ 'phase=6',
>                     X > 0 & Y > 0 & Y < X ~ 'phase=5',
>                     TRUE ~ 'unknown'
>   )
> )
>
> > input
> # A tibble: 2 x 6
>    YEAR   DAY      X     Y   Sig phase
>   <dbl> <dbl>  <dbl> <dbl> <dbl> <chr>
> 1  1981     9 -0.213  1.08  1.1  phase=8
> 2  1981    10  0.065  1.05  1.05 phase=6
>
> Jim Holtman
> *Data Munger Guru*
>
>
> *What is the problem that you are trying to solve?Tell me what you want to
> do, not how you want to do it.*
>
>
> On Tue, Oct 22, 2019 at 9:43 AM Yeasmin Alea <[hidden email]>
> wrote:
>
>> Hello Team
>> I would like to add a new column (for example-Phase) from the below data
>> set based on the conditions
>>    YEAR   DAY      X     Y   Sig
>>  1  1981     9 -0.213 1.08   1.10
>>  2  1981    10  0.065 1.05   1.05
>> *Conditions*
>>
>> D$Phase=sapply(D,function(a,b) {
>>      a <-D$X
>>      b<-D$Y
>>      if (a<0 && b<0 && b<a)
>>     {phase=1} else if (a<0 && b<0 && b>a)
>>     {phase=2} else if (a<0 && b>0 && b<a)
>>     {phase=7} else if (a<0 && b>0 && b>a)
>>     {phase=8} else if (a>0 && b<0 && b<a)
>>     {phase=3} else if (a>0 && b<0 && b>a)
>>     {phase=4} else if (a>0 && b>0 && b>a)
>>     {phase=6} else (a>0 && b>0 && b<a)
>>     {phase=5}
>> })
>>
>> Can anyone help to fix the script to get a Phase column based on the
>> conditions. The table will be like the below
>>    YEAR   DAY      X     Y   Sig      Phase
>>  1  1981     9 -0.213 1.08   1.10   phase=7
>>  2  1981    10  0.065 1.05   1.05   phase=6
>>
>> Many thanks
>> Alea
>>
>>         [[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.
>>
>

        [[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: Conditions in R (Help Post)

jholtman
Here is another way of doing it by computing the index based on the
conditions

> input <- read_delim(" YEAR   DAY      X     Y   Sig
+   1981     9 -0.213 1.08   1.10
+   1981    10  0.065 1.05   1.05", delim = ' ', trim_ws = TRUE)
>
> input <- mutate(input,
+   phase = case_when(X < 0 & Y < 0 & Y < X ~ 'phase=1',
+                     X < 0 & Y < 0 & Y > X ~ 'phase=2',
+                     X < 0 & Y > 0 & Y < X ~ 'phase=7',
+                     X < 0 & Y > 0 & Y > X ~ 'phase=8',
+                     X > 0 & Y < 0 & Y < X ~ 'phase=3',
+                     X > 0 & Y < 0 & Y > X ~ 'phase=4',
+                     X > 0 & Y > 0 & Y > X ~ 'phase=6',
+                     X > 0 & Y > 0 & Y < X ~ 'phase=5',
+                     TRUE ~ 'unknown'
+   )
+ )
> input
# A tibble: 2 x 6
   YEAR   DAY      X     Y   Sig phase
  <dbl> <dbl>  <dbl> <dbl> <dbl> <chr>
1  1981     9 -0.213  1.08  1.1  phase=8
2  1981    10  0.065  1.05  1.05 phase=6
>
> # another way of doing it by constructing an integer to be used as
> # index for the phase value based on the evaluation of X<0, Y<0 and Y<X.
>
> index <- with(input,
+               (X < 0) * 4 + (Y < 0) * 2 + (Y < X)
+ )
>
> phase_val <- c(6, 5, 4, 3, 8, 7, 2, 1)
>
> input$phase_2 <- paste0('phase=', phase_val[index + 1L])
> input
# A tibble: 2 x 7
   YEAR   DAY      X     Y   Sig phase   phase_2
  <dbl> <dbl>  <dbl> <dbl> <dbl> <chr>   <chr>
1  1981     9 -0.213  1.08  1.1  phase=8 phase=8
2  1981    10  0.065  1.05  1.05 phase=6 phase=6
>

Jim Holtman
*Data Munger Guru*


*What is the problem that you are trying to solve?Tell me what you want to
do, not how you want to do it.*


On Tue, Oct 22, 2019 at 11:28 AM jim holtman <[hidden email]> wrote:

> Had the condition for phase=2 incorrect:
>
> library(tidyverse)
> input <- read_delim(" YEAR   DAY      X     Y   Sig
>   1981     9 -0.213 1.08   1.10
>   1981    10  0.065 1.05   1.05", delim = ' ', trim_ws = TRUE)
>
> input <- mutate(input,
>   phase = case_when(X < 0 & Y < 0 & Y < X ~ 'phase=1',
>                     X < 0 & Y < 0 & Y > X ~ 'phase=2',
>                     X < 0 & Y > 0 & Y < X ~ 'phase=7',
>                     X < 0 & Y > 0 & Y > X ~ 'phase=8',
>                     X > 0 & Y < 0 & Y < X ~ 'phase=3',
>                     X > 0 & Y < 0 & Y > X ~ 'phase=4',
>                     X > 0 & Y > 0 & Y > X ~ 'phase=6',
>                     X > 0 & Y > 0 & Y < X ~ 'phase=5',
>                     TRUE ~ 'unknown'
>   )
> )
>
> Jim Holtman
> *Data Munger Guru*
>
>
> *What is the problem that you are trying to solve?Tell me what you want to
> do, not how you want to do it.*
>
>
> On Tue, Oct 22, 2019 at 11:20 AM jim holtman <[hidden email]> wrote:
>
>> Here is one way of doing it; I think the output you show is wrong:
>>
>> library(tidyverse)
>> input <- read_delim(" YEAR   DAY      X     Y   Sig
>>   1981     9 -0.213 1.08   1.10
>>   1981    10  0.065 1.05   1.05", delim = ' ', trim_ws = TRUE)
>>
>> input <- mutate(input,
>>   phase = case_when(X < 0 & Y < 0 & Y < X ~ 'phase=1',
>>                     X < 0 & Y > 0 & Y < X ~ 'phase=2',
>>                     X < 0 & Y > 0 & Y < X ~ 'phase=7',
>>                     X < 0 & Y > 0 & Y > X ~ 'phase=8',
>>                     X > 0 & Y < 0 & Y < X ~ 'phase=3',
>>                     X > 0 & Y < 0 & Y > X ~ 'phase=4',
>>                     X > 0 & Y > 0 & Y > X ~ 'phase=6',
>>                     X > 0 & Y > 0 & Y < X ~ 'phase=5',
>>                     TRUE ~ 'unknown'
>>   )
>> )
>>
>> > input
>> # A tibble: 2 x 6
>>    YEAR   DAY      X     Y   Sig phase
>>   <dbl> <dbl>  <dbl> <dbl> <dbl> <chr>
>> 1  1981     9 -0.213  1.08  1.1  phase=8
>> 2  1981    10  0.065  1.05  1.05 phase=6
>>
>> Jim Holtman
>> *Data Munger Guru*
>>
>>
>> *What is the problem that you are trying to solve?Tell me what you want
>> to do, not how you want to do it.*
>>
>>
>> On Tue, Oct 22, 2019 at 9:43 AM Yeasmin Alea <[hidden email]>
>> wrote:
>>
>>> Hello Team
>>> I would like to add a new column (for example-Phase) from the below data
>>> set based on the conditions
>>>    YEAR   DAY      X     Y   Sig
>>>  1  1981     9 -0.213 1.08   1.10
>>>  2  1981    10  0.065 1.05   1.05
>>> *Conditions*
>>>
>>> D$Phase=sapply(D,function(a,b) {
>>>      a <-D$X
>>>      b<-D$Y
>>>      if (a<0 && b<0 && b<a)
>>>     {phase=1} else if (a<0 && b<0 && b>a)
>>>     {phase=2} else if (a<0 && b>0 && b<a)
>>>     {phase=7} else if (a<0 && b>0 && b>a)
>>>     {phase=8} else if (a>0 && b<0 && b<a)
>>>     {phase=3} else if (a>0 && b<0 && b>a)
>>>     {phase=4} else if (a>0 && b>0 && b>a)
>>>     {phase=6} else (a>0 && b>0 && b<a)
>>>     {phase=5}
>>> })
>>>
>>> Can anyone help to fix the script to get a Phase column based on the
>>> conditions. The table will be like the below
>>>    YEAR   DAY      X     Y   Sig      Phase
>>>  1  1981     9 -0.213 1.08   1.10   phase=7
>>>  2  1981    10  0.065 1.05   1.05   phase=6
>>>
>>> Many thanks
>>> Alea
>>>
>>>         [[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.
>>>
>>

        [[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: Conditions in R (Help Post)

Bert Gunter-2
In reply to this post by jholtman
Both your syntax and semantics are wrong. This indicates to me that you
should spend more time with some basic R tutorials before proceeding.

That said, here are some of the errors:

1) You are not using sapply correctly. Moreover, no R level iteration is
needed anyway (sapply() iterates over columns in a data frame).
2) You are confusing && (not vectorized) with & (vectorized)
3) You seem to be confusing if ... else (flow control) with ifelse()
(vectorized function)
4) Many of your conditions are nonsense or redundant. For example:
 (a<0 & b>0 & b<a)  is nonsense
 (a<0 & b>0 & b>a)  is the same as (a < 0 & b > 0)

etc.

A simple way to accomplish what you want, I think, that takes advantage of
coercion of logicals to numeric
is something along the lines of this reproducible example:

set.seed(4444)
a <- floor(runif(50,-20,20))   ## D$X
b <- floor(runif(50,-20,20))   ## D$Y

D$phase <- rep_len(0, length(b)) +
   1*(a<0 & b<0 & b<a) +
   2*(a<0 & b<0 & b>a) +
   8*(a<0 & b>0)   ## etc.


Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Tue, Oct 22, 2019 at 11:30 AM jim holtman <[hidden email]> wrote:

> Had the condition for phase=2 incorrect:
>
> library(tidyverse)
> input <- read_delim(" YEAR   DAY      X     Y   Sig
>   1981     9 -0.213 1.08   1.10
>   1981    10  0.065 1.05   1.05", delim = ' ', trim_ws = TRUE)
>
> input <- mutate(input,
>   phase = case_when(X < 0 & Y < 0 & Y < X ~ 'phase=1',
>                     X < 0 & Y < 0 & Y > X ~ 'phase=2',
>                     X < 0 & Y > 0 & Y < X ~ 'phase=7',
>                     X < 0 & Y > 0 & Y > X ~ 'phase=8',
>                     X > 0 & Y < 0 & Y < X ~ 'phase=3',
>                     X > 0 & Y < 0 & Y > X ~ 'phase=4',
>                     X > 0 & Y > 0 & Y > X ~ 'phase=6',
>                     X > 0 & Y > 0 & Y < X ~ 'phase=5',
>                     TRUE ~ 'unknown'
>   )
> )
>
> Jim Holtman
> *Data Munger Guru*
>
>
> *What is the problem that you are trying to solve?Tell me what you want to
> do, not how you want to do it.*
>
>
> On Tue, Oct 22, 2019 at 11:20 AM jim holtman <[hidden email]> wrote:
>
> > Here is one way of doing it; I think the output you show is wrong:
> >
> > library(tidyverse)
> > input <- read_delim(" YEAR   DAY      X     Y   Sig
> >   1981     9 -0.213 1.08   1.10
> >   1981    10  0.065 1.05   1.05", delim = ' ', trim_ws = TRUE)
> >
> > input <- mutate(input,
> >   phase = case_when(X < 0 & Y < 0 & Y < X ~ 'phase=1',
> >                     X < 0 & Y > 0 & Y < X ~ 'phase=2',
> >                     X < 0 & Y > 0 & Y < X ~ 'phase=7',
> >                     X < 0 & Y > 0 & Y > X ~ 'phase=8',
> >                     X > 0 & Y < 0 & Y < X ~ 'phase=3',
> >                     X > 0 & Y < 0 & Y > X ~ 'phase=4',
> >                     X > 0 & Y > 0 & Y > X ~ 'phase=6',
> >                     X > 0 & Y > 0 & Y < X ~ 'phase=5',
> >                     TRUE ~ 'unknown'
> >   )
> > )
> >
> > > input
> > # A tibble: 2 x 6
> >    YEAR   DAY      X     Y   Sig phase
> >   <dbl> <dbl>  <dbl> <dbl> <dbl> <chr>
> > 1  1981     9 -0.213  1.08  1.1  phase=8
> > 2  1981    10  0.065  1.05  1.05 phase=6
> >
> > Jim Holtman
> > *Data Munger Guru*
> >
> >
> > *What is the problem that you are trying to solve?Tell me what you want
> to
> > do, not how you want to do it.*
> >
> >
> > On Tue, Oct 22, 2019 at 9:43 AM Yeasmin Alea <[hidden email]>
> > wrote:
> >
> >> Hello Team
> >> I would like to add a new column (for example-Phase) from the below data
> >> set based on the conditions
> >>    YEAR   DAY      X     Y   Sig
> >>  1  1981     9 -0.213 1.08   1.10
> >>  2  1981    10  0.065 1.05   1.05
> >> *Conditions*
> >>
> >> D$Phase=sapply(D,function(a,b) {
> >>      a <-D$X
> >>      b<-D$Y
> >>      if (a<0 && b<0 && b<a)
> >>     {phase=1} else if (a<0 && b<0 && b>a)
> >>     {phase=2} else if (a<0 && b>0 && b<a)
> >>     {phase=7} else if (a<0 && b>0 && b>a)
> >>     {phase=8} else if (a>0 && b<0 && b<a)
> >>     {phase=3} else if (a>0 && b<0 && b>a)
> >>     {phase=4} else if (a>0 && b>0 && b>a)
> >>     {phase=6} else (a>0 && b>0 && b<a)
> >>     {phase=5}
> >> })
> >>
> >> Can anyone help to fix the script to get a Phase column based on the
> >> conditions. The table will be like the below
> >>    YEAR   DAY      X     Y   Sig      Phase
> >>  1  1981     9 -0.213 1.08   1.10   phase=7
> >>  2  1981    10  0.065 1.05   1.05   phase=6
> >>
> >> Many thanks
> >> Alea
> >>
> >>         [[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.
> >>
> >
>
>         [[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.
>

        [[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: Conditions in R (Help Post)

Jim Lemon-4
In reply to this post by Yeasmin Alea
Hi Yeasmin,
I suspect that you didn't intend to have conditions like:

a<0 && b>0 && b<a)

I'm going to guess that you meant:

a < 0 && b > 0 && abs(b) < abs(a)

If this is the case, the following function seems to return the values
of phase that you want:

assign_phase<-function(x,y) {
 phase<-c(1,2,7,8,3,4,6,5)
 phase_index<-4 * (x > 0) + 2 * (y > 0) + (abs(x) < abs(y))
 return(phase[phase_index+1])
}

Jim

On Wed, Oct 23, 2019 at 3:43 AM Yeasmin Alea <[hidden email]> wrote:

>
> Hello Team
> I would like to add a new column (for example-Phase) from the below data
> set based on the conditions
>    YEAR   DAY      X     Y   Sig
>  1  1981     9 -0.213 1.08   1.10
>  2  1981    10  0.065 1.05   1.05
> *Conditions*
>
> D$Phase=sapply(D,function(a,b) {
>      a <-D$X
>      b<-D$Y
>      if (a<0 && b<0 && b<a)
>     {phase=1} else if (a<0 && b<0 && b>a)
>     {phase=2} else if (a<0 && b>0 && b<a)
>     {phase=7} else if (a<0 && b>0 && b>a)
>     {phase=8} else if (a>0 && b<0 && b<a)
>     {phase=3} else if (a>0 && b<0 && b>a)
>     {phase=4} else if (a>0 && b>0 && b>a)
>     {phase=6} else (a>0 && b>0 && b<a)
>     {phase=5}
> })
>
> Can anyone help to fix the script to get a Phase column based on the
> conditions. The table will be like the below
>    YEAR   DAY      X     Y   Sig      Phase
>  1  1981     9 -0.213 1.08   1.10   phase=7
>  2  1981    10  0.065 1.05   1.05   phase=6
>
> Many thanks
> Alea
>
>         [[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.

______________________________________________
[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: Conditions in R (Help Post)

Yeasmin Alea
In reply to this post by jholtman
Thank you. Can you please have a look the below data sets, script and
question?

*Dataset-1: Pen*

*YEAR   DAY      X     Y   Sig phase  *

*  <dbl> <dbl>  <dbl> <dbl> <dbl> <chr>  *

*1  1981     9 -0.213 1.08   1.10 Phase-7*

*2  1981    10  0.065 1.05   1.05 Phase-6*



*Dataset-2: Book*

*YEAR         Time *

*1  1981     1981-12-03 06:00:00 *

*2  1981    1981-12-04 00:00:00*



I want the output table as

*YEAR         Time                                   phase*

*1  1981     1981-12-03 06:00:00     Phase-7*

*2  1981    1981-12-04 00:00:00     Phase-4*



*How can I combine and match the Dataset-1 DAY (365 days*35 years) +YEAR
with Dataset-2 YEAR+Time? Dataset 1 has 5,551 rows and dataset 2 has 22,210*

d$Pen<-Pen[cbind(match(Book$Time,Pen$DAY)]
Kind regards
Alea Yeasmin


On Wed, Oct 23, 2019 at 2:20 AM jim holtman <[hidden email]> wrote:

> Here is one way of doing it; I think the output you show is wrong:
>
> library(tidyverse)
> input <- read_delim(" YEAR   DAY      X     Y   Sig
>   1981     9 -0.213 1.08   1.10
>   1981    10  0.065 1.05   1.05", delim = ' ', trim_ws = TRUE)
>
> input <- mutate(input,
>   phase = case_when(X < 0 & Y < 0 & Y < X ~ 'phase=1',
>                     X < 0 & Y > 0 & Y < X ~ 'phase=2',
>                     X < 0 & Y > 0 & Y < X ~ 'phase=7',
>                     X < 0 & Y > 0 & Y > X ~ 'phase=8',
>                     X > 0 & Y < 0 & Y < X ~ 'phase=3',
>                     X > 0 & Y < 0 & Y > X ~ 'phase=4',
>                     X > 0 & Y > 0 & Y > X ~ 'phase=6',
>                     X > 0 & Y > 0 & Y < X ~ 'phase=5',
>                     TRUE ~ 'unknown'
>   )
> )
>
> > input
> # A tibble: 2 x 6
>    YEAR   DAY      X     Y   Sig phase
>   <dbl> <dbl>  <dbl> <dbl> <dbl> <chr>
> 1  1981     9 -0.213  1.08  1.1  phase=8
> 2  1981    10  0.065  1.05  1.05 phase=6
>
> Jim Holtman
> *Data Munger Guru*
>
>
> *What is the problem that you are trying to solve?Tell me what you want to
> do, not how you want to do it.*
>
>
> On Tue, Oct 22, 2019 at 9:43 AM Yeasmin Alea <[hidden email]>
> wrote:
>
>> Hello Team
>> I would like to add a new column (for example-Phase) from the below data
>> set based on the conditions
>>    YEAR   DAY      X     Y   Sig
>>  1  1981     9 -0.213 1.08   1.10
>>  2  1981    10  0.065 1.05   1.05
>> *Conditions*
>>
>> D$Phase=sapply(D,function(a,b) {
>>      a <-D$X
>>      b<-D$Y
>>      if (a<0 && b<0 && b<a)
>>     {phase=1} else if (a<0 && b<0 && b>a)
>>     {phase=2} else if (a<0 && b>0 && b<a)
>>     {phase=7} else if (a<0 && b>0 && b>a)
>>     {phase=8} else if (a>0 && b<0 && b<a)
>>     {phase=3} else if (a>0 && b<0 && b>a)
>>     {phase=4} else if (a>0 && b>0 && b>a)
>>     {phase=6} else (a>0 && b>0 && b<a)
>>     {phase=5}
>> })
>>
>> Can anyone help to fix the script to get a Phase column based on the
>> conditions. The table will be like the below
>>    YEAR   DAY      X     Y   Sig      Phase
>>  1  1981     9 -0.213 1.08   1.10   phase=7
>>  2  1981    10  0.065 1.05   1.05   phase=6
>>
>> Many thanks
>> Alea
>>
>>         [[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.
>>
>

        [[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: Conditions in R (Help Post)

David Winsemius

On 10/22/19 10:19 PM, Yeasmin Alea wrote:

> Thank you. Can you please have a look the below data sets, script and
> question?
>
> *Dataset-1: Pen*
>
> *YEAR   DAY      X     Y   Sig phase  *
>
> *  <dbl> <dbl>  <dbl> <dbl> <dbl> <chr>  *
>
> *1  1981     9 -0.213 1.08   1.10 Phase-7*
>
> *2  1981    10  0.065 1.05   1.05 Phase-6*
>
>
>
> *Dataset-2: Book*
>
> *YEAR         Time *
>
> *1  1981     1981-12-03 06:00:00 *
>
> *2  1981    1981-12-04 00:00:00*
>
>
>
> I want the output table as
>
> *YEAR         Time                                   phase*
>
> *1  1981     1981-12-03 06:00:00     Phase-7*
>
> *2  1981    1981-12-04 00:00:00     Phase-4*


You are posting in HTML. R help is a plain text mailing list. It is easy
to send plain text using gmail. You should start over by configuring
your mail client for this purpose and send the output of dput(head(Pen))
and dput(head(Book)) rather than the versions above which do not lend
themselves to simple input strategies.


--

David.

>
>
>
> *How can I combine and match the Dataset-1 DAY (365 days*35 years) +YEAR
> with Dataset-2 YEAR+Time? Dataset 1 has 5,551 rows and dataset 2 has 22,210*
>
> d$Pen<-Pen[cbind(match(Book$Time,Pen$DAY)]
> Kind regards
> Alea Yeasmin
>
>
> On Wed, Oct 23, 2019 at 2:20 AM jim holtman <[hidden email]> wrote:
>
>> Here is one way of doing it; I think the output you show is wrong:
>>
>> library(tidyverse)
>> input <- read_delim(" YEAR   DAY      X     Y   Sig
>>    1981     9 -0.213 1.08   1.10
>>    1981    10  0.065 1.05   1.05", delim = ' ', trim_ws = TRUE)
>>
>> input <- mutate(input,
>>    phase = case_when(X < 0 & Y < 0 & Y < X ~ 'phase=1',
>>                      X < 0 & Y > 0 & Y < X ~ 'phase=2',
>>                      X < 0 & Y > 0 & Y < X ~ 'phase=7',
>>                      X < 0 & Y > 0 & Y > X ~ 'phase=8',
>>                      X > 0 & Y < 0 & Y < X ~ 'phase=3',
>>                      X > 0 & Y < 0 & Y > X ~ 'phase=4',
>>                      X > 0 & Y > 0 & Y > X ~ 'phase=6',
>>                      X > 0 & Y > 0 & Y < X ~ 'phase=5',
>>                      TRUE ~ 'unknown'
>>    )
>> )
>>
>>> input
>> # A tibble: 2 x 6
>>     YEAR   DAY      X     Y   Sig phase
>>    <dbl> <dbl>  <dbl> <dbl> <dbl> <chr>
>> 1  1981     9 -0.213  1.08  1.1  phase=8
>> 2  1981    10  0.065  1.05  1.05 phase=6
>>
>> Jim Holtman
>> *Data Munger Guru*
>>
>>
>> *What is the problem that you are trying to solve?Tell me what you want to
>> do, not how you want to do it.*
>>
>>
>> On Tue, Oct 22, 2019 at 9:43 AM Yeasmin Alea <[hidden email]>
>> wrote:
>>
>>> Hello Team
>>> I would like to add a new column (for example-Phase) from the below data
>>> set based on the conditions
>>>     YEAR   DAY      X     Y   Sig
>>>   1  1981     9 -0.213 1.08   1.10
>>>   2  1981    10  0.065 1.05   1.05
>>> *Conditions*
>>>
>>> D$Phase=sapply(D,function(a,b) {
>>>       a <-D$X
>>>       b<-D$Y
>>>       if (a<0 && b<0 && b<a)
>>>      {phase=1} else if (a<0 && b<0 && b>a)
>>>      {phase=2} else if (a<0 && b>0 && b<a)
>>>      {phase=7} else if (a<0 && b>0 && b>a)
>>>      {phase=8} else if (a>0 && b<0 && b<a)
>>>      {phase=3} else if (a>0 && b<0 && b>a)
>>>      {phase=4} else if (a>0 && b>0 && b>a)
>>>      {phase=6} else (a>0 && b>0 && b<a)
>>>      {phase=5}
>>> })
>>>
>>> Can anyone help to fix the script to get a Phase column based on the
>>> conditions. The table will be like the below
>>>     YEAR   DAY      X     Y   Sig      Phase
>>>   1  1981     9 -0.213 1.08   1.10   phase=7
>>>   2  1981    10  0.065 1.05   1.05   phase=6
>>>
>>> Many thanks
>>> Alea
>>>
>>>          [[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.
>>>
> [[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.

______________________________________________
[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.