Re: [R] if else statement adjustemtn

2020-06-15 Thread Ana Marija
HI Jim thank you so much! This is amazing answer!!! Ana On Sat, Jun 13, 2020 at 4:09 AM Jim Lemon wrote: > > Right, back from shopping. Since you have fourteen rows containing NAs > and you only want seven, we can infer that half of them must go. As > they are neatly divided into seven rows in

Re: [R] if else statement adjustemtn

2020-06-15 Thread Rasmus Liland
On 2020-06-13 19:09 +1000, Jim Lemon wrote: > Right, back from shopping. Since you have fourteen rows containing NAs > and you only want seven, we can infer that half of them must go. As > they are neatly divided into seven rows in which only one NA appears > and seven in which two stare meaningles

Re: [R] if else statement adjustemtn

2020-06-13 Thread Márk Szalai
Dear Ana, pmax could also fit here. pmax(b$FLASER, b$PLASER, na.rm = TRUE) Bests, Mark > -- > > Message: 21 > Date: Sat, 13 Jun 2020 19:09:11 +1000 > From: Jim Lemon > To: sokovic.anamar...@gmail.com > Cc: Rasmus Liland , r-help >

Re: [R] if else statement adjustemtn

2020-06-13 Thread Jim Lemon
Right, back from shopping. Since you have fourteen rows containing NAs and you only want seven, we can infer that half of them must go. As they are neatly divided into seven rows in which only one NA appears and seven in which two stare meaninglessly out at us. I will assume that the latter are the

Re: [R] if else statement adjustemtn

2020-06-12 Thread Ana Marija
Great idea! Here it is: > b[is.na(b$FLASER) | is.na(b$PLASER),] FID IID FLASER PLASER pheno 1: fam1837 G1837 1 NA 2 2: fam2410 G2410 NA NA 2 3: fam2838 G2838 NA 2 2 4: fam3367 G3367 1 NA 2 5: fam3410 G3410 1 NA 2 6: fam

Re: [R] if else statement adjustemtn

2020-06-12 Thread Jim Lemon
Since you have only a few troublesome NA values, if you look at them, or even better, post them: b[is.na(b$FLASER) | is.na(b$PLASER),] perhaps we can work out the appropriate logic to get rid of only the ones you don't want. Jim On Sat, Jun 13, 2020 at 12:50 PM Ana Marija wrote: > > Hi Rasmus,

Re: [R] if else statement adjustemtn

2020-06-12 Thread Ana Marija
Hi Rasmus, thank you for getting back to be, the command your provided seems to add all 11 NAs to 2s > b$pheno <- + ifelse(b$PLASER==2 | + b$FLASER==2 | + is.na(b$PLASER) | + is.na(b$PLASER) & b$FLASER %in% 1:2 | + is.na

Re: [R] if else statement adjustemtn

2020-06-12 Thread Rasmus Liland
On 2020-06-13 11:30 +1000, Jim Lemon wrote: > On Fri, Jun 12, 2020 at 8:06 PM Jim Lemon wrote: > > On Sat, Jun 13, 2020 at 10:46 AM Ana Marija wrote: > > > > > > I am trying to make a new column > > > "pheno" so that I reduce the number > > > of NAs > > > > it looks like those two NA values in >

Re: [R] if else statement adjustemtn

2020-06-12 Thread Jim Lemon
Obviously my guess was wrong. I thought you wanted to impute the value of "pheno" from FLASER if PLASER was missing. From just your summary table, it's hard to guess the distribution of NA values. My guess that the two undesirable NAs were cases where PLASER was missing and FLASER was 2. My tactic

Re: [R] if else statement adjustemtn

2020-06-12 Thread Ana Marija
Hi Jim, I tried it: > b$pheno<-ifelse(b$PLASER==2 | b$FLASER==2 |is.na(b$PLASER) & b$FLASER == > 2,2,1) > table(b$pheno,exclude = NULL) 12 859 828 11 > b$pheno<-ifelse(b$PLASER==2 | b$FLASER==2 |is.na(b$FLASER) & b$PLASER == > 2,2,1) > table(b$pheno,exclude = NULL) 12 859

Re: [R] if else statement adjustemtn

2020-06-12 Thread Jim Lemon
Hi Ana, >From your desired result, it looks like those two NA values in PLASER are the ones you want to drop. If so, try this: b$pheno<-ifelse(b$PLASER==2 | b$FLASER==2 | is.na(b$PLASER) & b$FLASER == 2,2,1) and if I have it the wrong way round, swap FLASER and PLASER in the bit I have added. J

Re: [R] if else statement

2020-05-05 Thread PIKAL Petr
Hi another possible version b$pheno <- ((b$FLASER==2) | (b$PLASER==2))+1 Cheers Petr > -Original Message- > From: R-help On Behalf Of Rui Barradas > Sent: Monday, May 4, 2020 8:32 PM > To: sokovic.anamar...@gmail.com; r-help > Subject: Re: [R] if else statement >

Re: [R] if else statement

2020-05-04 Thread Richard O'Keefe
Your ifelse expression looks fine. What goes wrong with it? On Tue, 5 May 2020 at 05:16, Ana Marija wrote: > > Hello, > > I have a data frame like this: > > > head(b) >FID IID FLASER PLASER > 1: fam1000 G1000 1 1 > 2: fam1001 G1001 1 1 > 3: fam1003 G1003 1

Re: [R] if else statement

2020-05-04 Thread Rui Barradas
Hello, Here is a way, using logical indices. b$pheno <- NA b$pheno[b$FLASER == 1 & b$PLASER == 1] <- 1 b$pheno[b$FLASER == 2 | b$PLASER == 2] <- 2 Hope this helps, Rui Barradas Às 18:15 de 04/05/20, Ana Marija escreveu: Hello, I have a data frame like this: head(b) FID IID FLA

Re: [R] if else statement

2020-05-04 Thread Ana Marija
Thank you for the tip about table function, it seems correct: > table(b$FLASER, b$PLASER, exclude = NULL) 1 2 1836 6916 2 14 708 0 45 28 > table(b$pheno,exclude = NULL) 12 836 828 34 On Mon, May 4, 2020 at 12:45 PM Jeff Newmiller wrote: > T

Re: [R] if else statement

2020-05-04 Thread Jeff Newmiller
To expand on Patrick's response... You can use the expand.grid function to generate a test table containing all combinations. However, we would not be in a position to verify that the results you get when you apply your logic to the test table are what you want... you know the requirements much

Re: [R] if else statement

2020-05-04 Thread Patrick (Malone Quantitative)
"I tried this but I am not sure if this is correct:" Does it provide the expected result for all possible combinations of 1/2/NA for both variables? On Mon, May 4, 2020 at 1:16 PM Ana Marija wrote: > Hello, > > I have a data frame like this: > > > head(b) >FID IID FLASER PLASER > 1: f

Re: [R] if else statement for rain data to define zero for dry and one to wet

2015-06-07 Thread roslinazairimah zakaria
Dear all, All works well. Thank you so much for your help. D## Function 1 wet_dry1 <- function(x,thresh=0.1) { for(column in 1:dim(x)[2]) x[,column] <- ifelse(x[,column]>=thresh,1,0) return(x) } wet_dry1(dt) ## Function 2 wet_dry2 <- ( dt >= 0.1)*1 wet_dry2 wet_total <- colSums(wet_dry2) p

Re: [R] if else statement for rain data to define zero for dry and one to wet

2015-06-06 Thread William Dunlap
Your f1() has an unneeded for loop in it. f1a <- function(mat) mat > 0.1, 1, 0) would do the same thing in a bit less time. However, I think that a simple mat > 0.1 would be preferable. The resulting TRUEs and FALSEs are easier to interpret than the 1s and 0s that f1a() produces and arithme

Re: [R] if else statement for rain data to define zero for dry and one to wet

2015-06-06 Thread Dennis Murphy
I'm sorry, but I have to take issue with this particular use case of ifelse(). When the goal is to generate a logical vector, ifelse() is very inefficient. It's better to apply a logical condition directly to the object in question and multiply the result by 1 to make it numeric/integer rather than

Re: [R] if else statement for rain data to define zero for dry and one to wet

2015-06-06 Thread roslinazairimah zakaria
Thank you jim. On Saturday, June 6, 2015, Jim Lemon wrote: > Hi rosalinazairimah, > I think the problem is that you are using "if" instead of "ifelse". Try > this: > > wet_dry<-function(x,thresh=0.1) { > for(column in 1:dim(x)[2]) x[,column]<-ifelse(x[,column]>=thresh,1,0) > return(x) > } > we

Re: [R] if else statement for rain data to define zero for dry and one to wet

2015-06-06 Thread Jim Lemon
Hi rosalinazairimah, I think the problem is that you are using "if" instead of "ifelse". Try this: wet_dry<-function(x,thresh=0.1) { for(column in 1:dim(x)[2]) x[,column]<-ifelse(x[,column]>=thresh,1,0) return(x) } wet_dry(dt) and see what you get. Also, why can I read your message perfectly w

Re: [R] if else statement for rain data to define zero for dry and one to wet

2015-06-05 Thread John Kane
Please do not post in HTML. It made your posting unreadable. R-help is a plain text list and when it removes all the HTML tags often the result is gibberish Have a look at http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example and http://adv-r.had.co.nz/Reproduci

Re: [R] if else statement for rain data to define zero for dry and one to wet

2015-06-05 Thread PIKAL Petr
Hi I will not inspect your function as it is corrupted by HTML posting. If your data frame is named rain newrain <- (rain>.1)*1 gives you new data frame with reqired coding. However I am not sure, what do you want to do next. Do you want to merge those 2 data frames so as coded column is besi

Re: [R] if else statement in loop

2014-09-29 Thread PIKAL Petr
e(1:3, .Label = c("samas4", "samas5", "samas6" ), class = "factor")), .Names = c("FID", "IID"), class = "data.frame", row.names = c(NA, -3L)) > -Original Message- > From: Kate Ignatius [mailto:kate.ignat...@g

Re: [R] if else statement in loop

2014-09-29 Thread Kate Ignatius
Ooops, I edited the code wrong to make it more easier for interpretation and got X and Y's mixed up. Try this: for(i in length(1:(nrow(X{ Y$IID1new <- ifelse((as.character(Y[,2]) == as.character(X[,i]) & Y$IID1new != ''), as.character(as.matrix(X[,(nrow(X)+i)])),'') } The second should

Re: [R] if else statement in loop

2014-09-28 Thread PIKAL Petr
Hi Please, be more clear in what do you want. I get many errors trying your code and your explanation does not help much. > for(i in length(1:(2*nrow(X{ + Y$IID1new <- ifelse((as.character(Y[,2]) == as.characterXl[,i]) & X$IID1new != '') , as.character(as.matrix(X[,(2*nrow(X)+i)])),'')

Re: [R] if/else statement without curly brackets gives a problem

2012-01-21 Thread Florent D.
It is well explained here http://www.burns-stat.com/pages/Tutor/R_inferno.pdf page 67. On Sat, Jan 21, 2012 at 9:56 PM, Ery Arias-Castro wrote: > Hello, > > This example seems strange to me: > > > if (2 > 3) print('Yes'); else print('No') > Error: unexpected 'else' in " else" > > > {if (2 > 3) p

Re: [R] if/else statement without curly brackets gives a problem

2012-01-21 Thread jim holtman
Don't use the ';' > if (2 > 3) print('Yes'); else print('No') Error: unexpected 'else' in " else" No suitable frames for recover() > if (2 > 3) print('Yes') else print('No') [1] "No" > 'else' is part of the 'if'. The ';' implies the start of a new statement. You do not need the ';' when writing

Re: [R] if else statement error

2009-10-03 Thread Duncan Murdoch
On 03/10/2009 3:33 PM, Marianne Promberger wrote: Two problems with your code: (1) The "else" has to be on the same line as the closing "}" -- this was confusing for me as well. (2) "and" probably should be "&", but read up on logical operators: ?& Better to always use && in an if. Duncan M

Re: [R] if else statement error

2009-10-03 Thread Marianne Promberger
Two problems with your code: (1) The "else" has to be on the same line as the closing "}" -- this was confusing for me as well. (2) "and" probably should be "&", but read up on logical operators: ?& Try: ini=3 b=4 if (ini==1) { a=3 } else if (ini>1 & b>2 ) { a=3 } else { a=6 } HTH, Ma