Re: [R] Conditions

2019-11-26 Thread William Michels via R-help
Hi Val, Here's an answer using a series of ifelse() statements. Because the d4 column is created initially using NA as a placeholder, you can check your conditional logic at the end using table(!is.na(dat2$d4)): > dat2 <-read.table(text="ID d1 d2 d3 + A 0 25 35 + B 12 22 0 + C 0 0 31 + E 10 2

Re: [R] Conditions

2019-11-26 Thread Jim Lemon
Hi val, You had a "conditional leak" in your ifelse statements: dat2 <-read.table(text="ID d1 d2 d3 A 0 25 35 B 12 22 0 C 0 0 31 E 10 20 30 F 0 0 0", header=TRUE,stringsAsFactors=FALSE) dat2$d4<- ifelse(dat2$d1,dat2$d1,ifelse(dat2$d2,dat2$d2,ifelse(dat2$d3,dat2$d3,0))) Even though it works, it i

Re: [R] Conditions

2019-11-26 Thread Bert Gunter
I generally find nested ifelse's to be confusing and prone to error, so I usually prefer to proceed sequentially using subsetting with logicals or replicated, but not nested ifelse's. In your example, the translation to logical indexing seems pretty straightforward. Using your example: > dat2 <-w

[R] Conditions

2019-11-26 Thread Val
HI All, I am having a little issue in my ifelse statement, The data frame looks like as follow. dat2 <-read.table(text="ID d1 d2 d3 A 0 25 35 B 12 22 0 C 0 0 31 E 10 20 30 F 0 0 0",header=TRUE,stringsAsFactors=F) I want to create d4 and set the value based on the following conditions. If d1

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 &

Re: [R] conditions simpler way

2011-06-05 Thread Prof Brian Ripley
To drop the burnin period xx <- x1$x[-seq_len(burnin)] And it sounds like you want mean(xx > -1 & xx < 2] but your code and description differ. On Sun, 5 Jun 2011, Kehl Dániel wrote: Dear All, I have a MCMC result in x1. I was wondering if there is a simpler, more elegant way of evaluatin

[R] conditions simpler way

2011-06-05 Thread Kehl Dániel
Dear All, I have a MCMC result in x1. I was wondering if there is a simpler, more elegant way of evaluating the estimate of an integral then this (I am pretty sure there is): Also if I want to count the x's say -1in period. [code] z <- -2 burnin <- 2000 int1 <- length(x1$x[(burnin+1):length(