Re: [R] Conditions in R (Help Post)

2019-10-23 Thread 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 * * * *1 1981 9 -0.213 1.08 1.10 Phase-7* *2 198110 0.065 1.05 1.05 Phase-6* *Dataset-

Re: [R] Conditions in R (Help Post)

2019-10-23 Thread Yeasmin Alea
Thank you. Can you please have a look the below data sets, script and question? *Dataset-1: Pen* *YEAR DAY X Y Sig phase * * * *1 1981 9 -0.213 1.08 1.10 Phase-7* *2 198110 0.065 1.05 1.05 Phase-6* *Dataset-2: Book* *YEAR Time * *1 1981 1

Re: [R] Conditions in R (Help Post)

2019-10-22 Thread Jim Lemon
Hi Yeasmin, I suspect that you didn't intend to have conditions like: a<0 && b>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 >

Re: [R] Conditions in R (Help Post)

2019-10-22 Thread Bert Gunter
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 colu

Re: [R] Conditions in R (Help Post)

2019-10-22 Thread jim holtman
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 + 198110 0.065 1.05 1.05", delim = ' ', trim_ws = TRUE) > > input <- mutate(input, + phase = case_when(X < 0 & Y < 0 & Y

Re: [R] Conditions in R (Help Post)

2019-10-22 Thread jim holtman
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 198110 0.065 1.05 1.05", delim = ' ', trim_ws = TRUE) input <- mutate(input, phase = case_when(X < 0 & Y < 0 & Y < X ~ 'phase=1',

Re: [R] Conditions in R (Help Post)

2019-10-22 Thread jim holtman
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 198110 0.065 1.05 1.05", delim = ' ', trim_ws = TRUE) input <- mutate(input, phase = case_when(X < 0 & Y < 0 & Y < X

[R] Conditions in R (Help Post)

2019-10-22 Thread 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 198110 0.065 1.05 1.05 *Conditions* D$Phase=sapply(D,function(a,b) { a <-D$X b<-D$Y if (a<0 &