Re: [R] if-else that returns vector

2023-10-13 Thread Christofer Bogaso
This is very interesting. Thanks for sharing. On Fri, Oct 13, 2023 at 10:25 AM Richard O'Keefe wrote: > > ?ifelse > 'ifelse' returns a value with the same shape as 'test' which is > filled with elements selected from either 'yes' or 'no' depending > on whether the element of 'test' is '

Re: [R] if-else that returns vector

2023-10-12 Thread Richard O'Keefe
?ifelse 'ifelse' returns a value with the same shape as 'test' which is filled with elements selected from either 'yes' or 'no' depending on whether the element of 'test' is 'TRUE' or 'FALSE'. This is actually rather startling, because elsewhere in the S (R) language, operands are normal

Re: [R] if-else that returns vector

2023-10-12 Thread Rui Barradas
Às 21:22 de 12/10/2023, Christofer Bogaso escreveu: Hi, Following expression returns only the first element ifelse(T, c(1,2,3), c(5,6)) However I am looking for some one-liner expression like above which will return the entire vector. Is there any way to achieve this? ___

Re: [R] if-else that returns vector

2023-10-12 Thread Jeff Newmiller via R-help
What a strange question... ifelse returns a vector (all data in R is vectors... some have length 1, but length zero is also possible, as are longer vectors) that is exactly as long as the logical vector that you give it, filled with elements from the respective positions in the vectors supplied

Re: [R] if-else that returns vector

2023-10-12 Thread Ben Bolker
how about if(T) c(1,2,3) else c(5,6) ? On 2023-10-12 4:22 p.m., Christofer Bogaso wrote: Hi, Following expression returns only the first element ifelse(T, c(1,2,3), c(5,6)) However I am looking for some one-liner expression like above which will return the entire vector. Is there any way

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 with 4 conditions problem

2018-05-27 Thread Rui Barradas
Hello, It's just a sequence of ifelse instructions. dat <- read.table(text = " A B 1 1 1 0 0 1 0 0 ", header = TRUE) dat$A1 <- ifelse(dat$A == 1 & dat$B == 1, 1, 0) dat$A2 <- ifelse(dat$A == 1 & dat$B == 0, 1,

Re: [R] if/else help

2016-09-22 Thread Crombie, Burnette N
Thanks very much for your detailed reply to my post. Very helpful/useful tool(s) you’ve provide me. Best wishes, B. From: William Dunlap [mailto:wdun...@tibco.com] Sent: Wednesday, September 21, 2016 10:48 AM To: Crombie, Burnette N Cc: r-help@r-project.org Subject: Re: [R] if/else help If

Re: [R] if/else help

2016-09-22 Thread Crombie, Burnette N
ishes - B -Original Message- From: MacQueen, Don [mailto:macque...@llnl.gov] Sent: Wednesday, September 21, 2016 11:26 AM To: Crombie, Burnette N ; r-help@r-project.org Subject: Re: [R] if/else help Hopefully this is not a homework question. The other responses are fine, but I would sugges

Re: [R] if/else help

2016-09-21 Thread David Winsemius
> On Sep 21, 2016, at 8:26 AM, MacQueen, Don wrote: > > Hopefully this is not a homework question. > > The other responses are fine, but I would suggest the simplest way to do > exactly what you ask is > > > if (!exists('r4')) r4 <- data.frame(a=0, b=0, c=0, d='x') > > > The exists() functi

Re: [R] if/else help

2016-09-21 Thread MacQueen, Don
Hopefully this is not a homework question. The other responses are fine, but I would suggest the simplest way to do exactly what you ask is if (!exists('r4')) r4 <- data.frame(a=0, b=0, c=0, d='x') The exists() function requires a character string for its first argument, i.e., the name of the

Re: [R] if/else help

2016-09-21 Thread William Dunlap via R-help
If you write your code as functions you can avoid the nasty 'if(exists("x"))x<-...' business this by writing default values for arguments to your function. They will be computed only when they are used. E.g., analyzeData <- function(a=0, b=0, c=0, d="x", r4 = data.frame(a, b, c, d)) { summa

Re: [R] if/else help

2016-09-21 Thread David Winsemius
> On Sep 20, 2016, at 12:31 PM, Crombie, Burnette N wrote: > > If a data.frame (r4) does not exist in my R environment, I would like to > create it before I move on to the next step in my script. How do I make that > happen? Here is what I want to do from a code perspective: > > if (exists(r

Re: [R] if/else help

2016-09-21 Thread Jeff Newmiller
Get rid of the commas? Get rid of the get() function call? Get rid of the cbind() function call? Post using plain text format so the HTML doesn't screw up code? Read the Posting Guide? All of these ideas have merit IMHO... -- Sent from my phone. Please excuse my brevity. On September 20, 2016

Re: [R] if else condition - help

2016-05-23 Thread Martin Maechler
> jim holtman > on Sun, 22 May 2016 16:47:06 -0400 writes: > if you want to use 'ifelse', here is a way: hmm, why should he want that ? The OP did mention that it's about somewhat large objects, so efficiency is one of the considerations : ifelse() is often convenient and nicely

Re: [R] if else condition - help

2016-05-22 Thread jim holtman
if you want to use 'ifelse', here is a way: > k = + structure(c(0.0990217544905328, 1.60623837694539, -0.104331330281166, + -2.91485614212114, -0.108388742328104, -1.41670341534772, -1.70609114096417, + 2.92018951284015, 0.201868946570178, 0.907637296638577, -0.403004972105994, + -2.4771801580322

Re: [R] if else condition - help

2016-05-22 Thread Dylan Keenan
Try this: > sign(ifelse(abs(k)<=1.5, 0, k)) C1 C2 C3 C4 A 0 0 0 0 B 1 0 0 0 C 0 -1 0 0 D -1 1 -1 -1 On Sun, May 22, 2016 at 2:00 PM Adrian Johnson wrote: > Hi group: > I am having difficulty with if else condition. I kindly request some help. > > I have a matrix k > > > k >

Re: [R] if else condition - help

2016-05-22 Thread David Winsemius
> On May 22, 2016, at 11:23 AM, Adrian Johnson > wrote: > > Thank you both Dylan and Wray. > > since my matrix is quite large and for simplicity in downstream > operation, i will use sign function. thanks a lot. > > On Sun, May 22, 2016 at 2:12 PM, Dylan Keenan wrote: >> Try this: >> >>> si

Re: [R] if else condition - help

2016-05-22 Thread Adrian Johnson
Thank you both Dylan and Wray. since my matrix is quite large and for simplicity in downstream operation, i will use sign function. thanks a lot. On Sun, May 22, 2016 at 2:12 PM, Dylan Keenan wrote: > Try this: > >> sign(ifelse(abs(k)<=1.5, 0, k)) > > > C1 C2 C3 C4 > A 0 0 0 0 > B 1 0 0

Re: [R] if else condition - help

2016-05-22 Thread WRAY NICHOLAS
Hi Adrian I'm not sure that you need to use the ifelse here. You can simply assign values ina vector or matrix using a simple condition -- here is a simple example: v<-c(4,5,6,7) v1<-v v1[]<-0 v1[v<5]<--1 v1[v>6]<-1 v1 Nick > > On 22 May 2016 at 18:58 Adrian Johnson wrote: > > > Hi

Re: [R] If else

2015-10-31 Thread Rolf Turner
Ted: You are either being deliberately obtuse or playing Devil's advocate or just stirring. It is clear from his/her posts that the OP has limited understanding of both R and statistics. Your sophisticated philosophising about the possibility of "three sexes" is very unlikely to have anyth

Re: [R] If else

2015-10-31 Thread Jim Lemon
8:23:05 AM PDT, Val wrote: > >>>>> Hi All, > >>>>> > >>>>> > >>>>> Yes I need to change to numeric because I am preparing a data set > >>>>> for > >>>>> further analysis. The variable to be changed

Re: [R] If else

2015-10-31 Thread Duncan Murdoch
>>>>> numeric >>>>> (in this case, sex) will be a response variable. Some records have >>>>> missing >>>>> observation on sex and it is blank. >>>>> id sex >>>>> 1 >>>>> 2 >>>&g

Re: [R] If else

2015-10-31 Thread Val
on sex and it is blank. > >>> > id sex > >>> > 1 > >>> > 2 > >>> > 3 M > >>> > 4 F > >>> > 5 M > >>> > 6 F > >>> > 7 F > >>&g

Re: [R] If else

2015-10-31 Thread Ted Harding
F >>> > >>> >I am reading the data like this >>> > >>> >mydata <- read.csv(header=TRUE, text=', sep=", ") >>> > id sex >>> > 1 NA >>> > 2 NA >>> > 3 M >>

Re: [R] If else

2015-10-31 Thread Jeff Newmiller
>> > 7 F >> > >> >The data set is huge (>250,000) >> > >> > >> >I want the output like this >> > >> > id sexsex1 >> > 1 NA0 >> > 2 NA 0 >> > 3 M 1 >> &

Re: [R] If else

2015-10-31 Thread Val
F 2 > > 7 F 2 > > > >Thank you in advance > > > > > >On Sat, Oct 31, 2015 at 5:59 AM, John Kane wrote: > > > >> In line. > >> > >> John Kane > >> Kingston ON Canada > >> > >> > >> > -

Re: [R] If else

2015-10-31 Thread Jeff Newmiller
2 > >Thank you in advance > > >On Sat, Oct 31, 2015 at 5:59 AM, John Kane wrote: > >> In line. >> >> John Kane >> Kingston ON Canada >> >> >> > -Original Message- >> > From: valkr...@gmail.com >> > Sent:

Re: [R] If else

2015-10-31 Thread Val
ada > > > > -Original Message- > > From: valkr...@gmail.com > > Sent: Fri, 30 Oct 2015 20:40:03 -0500 > > To: istaz...@gmail.com > > Subject: Re: [R] If else > > > > I am trying to change the mydata$sex from character to numeric > > Wh

Re: [R] If else

2015-10-31 Thread John Kane
In line. John Kane Kingston ON Canada > -Original Message- > From: valkr...@gmail.com > Sent: Fri, 30 Oct 2015 20:40:03 -0500 > To: istaz...@gmail.com > Subject: Re: [R] If else > > I am trying to change the mydata$sex from character to numeric W

Re: [R] If else

2015-10-30 Thread Val
I am trying to change the mydata$sex from character to numeric I want teh out put like id sex 1 NA 0 2 NA 0 3 M 1 4 F 2 5 M1 6 F 2 7 F2 mydata$sex1 <- 0 if(mydata$sex =="M " ){ mydata$sex1<-1 } else { mydata$sex1<-2

Re: [R] If else

2015-10-30 Thread Ista Zahn
Using numeric for missing sounds like asking for trouble. But if you must, something like mydata$confusingWillCauseProblemsLater <- ifelse( is.na(mydata$sex), 0, as.numeric(factor(mydata$sex, levels = c("M", "F" should do it. Best, Ista On Fri, Oct 30, 20

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 for cumulative sum error

2014-12-03 Thread Jefferson Ferreira-Ferreira
ogy > Texas A&M University > College Station, TX 77840-4352 > > -Original Message- > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of David > Winsemius > Sent: Tuesday, December 2, 2014 2:50 PM > To: Jefferson Ferreira-Ferreira > Cc: r-help

Re: [R] if else for cumulative sum error

2014-12-02 Thread David L Carlson
ege Station, TX 77840-4352 -Original Message- From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of David Winsemius Sent: Tuesday, December 2, 2014 2:50 PM To: Jefferson Ferreira-Ferreira Cc: r-help@r-project.org Subject: Re: [R] if else for cumulative sum error On Dec 2, 2014, at 12:26 PM

Re: [R] if else for cumulative sum error

2014-12-02 Thread David Winsemius
On Dec 2, 2014, at 12:26 PM, Jefferson Ferreira-Ferreira wrote: > Thank you for replies. > > David, > > I tried your modified form > > for (i in 1:seq_along(rownames(dadosmax))){ No. it is either 1: or seq_along(...). in this case perhaps 1:(nrow(dadosmax)-44 would be safer You do not

Re: [R] if else for cumulative sum error

2014-12-02 Thread Jefferson Ferreira-Ferreira
Thank you for replies. David, I tried your modified form for (i in 1:seq_along(rownames(dadosmax))){ dadosmax$enchday[i] <- if ( (sum(dadosmax$above[i:(i+44)])) >= 45) 1 else 0 } However, I'm receiving this warning: Warning message: In 1:seq_along(rownames(dadosmax)) : numerical expression

Re: [R] if else for cumulative sum error

2014-12-02 Thread John McKown
On Tue, Dec 2, 2014 at 12:08 PM, Jefferson Ferreira-Ferreira < jeco...@gmail.com> wrote: > Hello everybody; > > I'm writing a code where part of it is as follows: > > for (i in nrow(dadosmax)){ > dadosmax$enchday[i] <- if (sum(dadosmax$above[i:(i+44)]) >= 45) 1 else 0 > } > ​Without some test d

Re: [R] if else for cumulative sum error

2014-12-02 Thread David Winsemius
On Dec 2, 2014, at 10:08 AM, Jefferson Ferreira-Ferreira wrote: > Hello everybody; > > I'm writing a code where part of it is as follows: > > for (i in nrow(dadosmax)){ > dadosmax$enchday[i] <- if (sum(dadosmax$above[i:(i+44)]) >= 45) 1 else 0 > } > > That is for each row of my data frame, su

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 in R

2013-11-19 Thread David Winsemius
On Nov 19, 2013, at 5:30 PM, Gary Dong wrote: > Dear R users, > > I am a R beginner and I am having trouble in using "If Else" in R. Here is > an example: > > ## create a data frame > > a<-c(1,2,3,4) > b<-c(2,0,5,0) > ab<-data.frame(cbind(a,b)) > > ##calculate c, which is the ratio between a

Re: [R] if else in R

2013-11-19 Thread Jeff Newmiller
Which means you should use the ifelse function... ab$c <- ifelse( ab$b>0, ab$a/ab$b, 0 ) --- Jeff NewmillerThe . . Go Live... DCN:Basics: ##.#. ##.#. Live Go...

Re: [R] if else in R

2013-11-19 Thread arun
Hi, Try: within(ab, {c <- ifelse(b>0, a/b,0)}) A.K. On Tuesday, November 19, 2013 8:51 PM, Gary Dong wrote: Dear R users, I am a R beginner and I am having trouble in using "If Else" in R. Here is an example: ## create a data frame a<-c(1,2,3,4) b<-c(2,0,5,0) ab<-data.frame(cbind(a,b)) ##c

Re: [R] if else in R

2013-11-19 Thread Ben Tupper
Hi, On Nov 19, 2013, at 9:41 PM, Nick Matzke wrote: > Hi, > > This would be an issue with if() as well as if/else. ab$b has 4 > numbers in it, so ab$b > 0 evaluates to "TRUE TRUE FALSE TRUE" or > whatever. if() can only take a single true or false. Cheers! Nick > As a follow up, you could ma

Re: [R] if else in R

2013-11-19 Thread Nick Matzke
Hi, This would be an issue with if() as well as if/else. ab$b has 4 numbers in it, so ab$b > 0 evaluates to "TRUE TRUE FALSE TRUE" or whatever. if() can only take a single true or false. Cheers! Nick On Tue, Nov 19, 2013 at 8:30 PM, Gary Dong wrote: > Dear R users, > > I am a R beginner and I a

Re: [R] If else loop problem: the condition has length > 1 and only the first element will be used

2013-08-26 Thread arun
y by: dat1$t.tr<- as.character(dat1$t.tr) A.K. - Original Message - From: "Fethe, Michael" To: Dennis Murphy ; "R-help@r-project.org" Cc: Sent: Monday, August 26, 2013 8:45 AM Subject: Re: [R] If else loop problem: the condition has length > 1 and only

Re: [R] If else loop problem: the condition has length > 1 and only the first element will be used

2013-08-26 Thread Fethe, Michael
689 -43.47 645.53 > #4 Pto 72 624 -68.17 555.83 > #5 Pto 72 666 -68.17 597.83 > #6 Pto 24 620 -29.39 590.61 > > > #or ?merge() > > A.K. > > > > > - Original Message - > From: Bert Gunter > To: arun > Cc: &q

Re: [R] If else loop problem: the condition has length > 1 and only the first element will be used

2013-08-26 Thread Fethe, Michael
a-j)? This code produces : In Ops.factor(signal2, 10.465507) : - not meaningful for factors Thanks for the input. ____________ From: Dennis Murphy Sent: Monday, August 26, 2013 4:59 AM To: Fethe, Michael Subject: Re: [R] If else loop problem: the condition has length > 1 and

Re: [R] If else loop problem: the condition has length > 1 and only the first element will be used

2013-08-26 Thread PIKAL Petr
nces@r- > project.org] On Behalf Of arun > Sent: Monday, August 26, 2013 8:32 AM > To: Fethe, Michael > Cc: R help > Subject: Re: [R] If else loop problem: the condition has length > 1 and > only the first element will be used > > HI, > > It may be better to provide

Re: [R] If else loop problem: the condition has length > 1 and only the first element will be used

2013-08-26 Thread arun
Cc: "Fethe, Michael" ; R help Sent: Monday, August 26, 2013 10:09 AM Subject: Re: [R] If else loop problem: the condition has length > 1 and only the first element will be used Suggestion: Don't do the ifelse stuff below. See ?switch  instead. -- Bert On Sun, Aug 25, 2013 at 1

Re: [R] If else loop problem: the condition has length > 1 and only the first element will be used

2013-08-26 Thread Bert Gunter
Suggestion: Don't do the ifelse stuff below. See ?switch instead. -- Bert On Sun, Aug 25, 2013 at 11:32 PM, arun wrote: > HI, > > It may be better to provide an example dataset using ?dput(). > dput(head(dataset),20) > > Try: > signal3<- ifelse(t.tr=="Pst 24", signal2-17.29, ifelse(t.tr=="Pst

Re: [R] If else loop problem: the condition has length > 1 and only the first element will be used

2013-08-25 Thread arun
HI, It may be better to provide an example dataset using ?dput(). dput(head(dataset),20) Try: signal3<- ifelse(t.tr=="Pst 24", signal2-17.29, ifelse(t.tr=="Pst 48", signal2 - 43.93256, etc.)) A.K. - Original Message - From: "Fethe, Michael" To: "r-help@r-project.org" Cc: Sent:

Re: [R] if else elseif for data frames

2012-08-14 Thread PIKAL Petr
Hi What about creating a new column with same factor and just chase its levels. dat2$col2<-dat2$col1 > levels(dat2$col2) [1] "high""low" "Neutral" > levels(dat2$col2)<-c("H", "L","N") > dat2 col1 col2 1 highH 2 NeutralN 3 NeutralN 4 lowL 5 highH 6

Re: [R] if else elseif for data frames

2012-08-13 Thread Rolf Turner
It seems to me that the "recode()" function from the "car" package is what you need. cheers, Rolf Turner On 13/08/12 13:07, Sachinthaka Abeywardana wrote: The thing is I have about 10 cases. I saw the ifelse statement but was wondering if there was a cleaner method of doing it. Th

Re: [R] if else elseif for data frames

2012-08-13 Thread arun
ot;low"="L"') dat2 #  col1 col2 #1 high    H #2  Neutral    N #3  Neutral    N #4  low    L #5 high    H #6  low    L #7  low    L #8  Neutral    N #9  Neutral    N #10    high    H A.K. ____________ From: Sachinthaka Abeywardana To:

Re: [R] if else elseif for data frames

2012-08-13 Thread arun
_ From: Sachinthaka Abeywardana To: arun Cc: R help Sent: Sunday, August 12, 2012 9:07 PM Subject: Re: [R] if else elseif for data frames The thing is I have about 10 cases. I saw the ifelse statement but was wondering if there was a cleaner method of doing it. The coding will get re

Re: [R] if else elseif for data frames

2012-08-12 Thread R. Michael Weylandt
On Sun, Aug 12, 2012 at 8:07 PM, Sachinthaka Abeywardana wrote: > The thing is I have about 10 cases. I saw the ifelse statement Note that there is no "ifelse" statement: there is only nested if/else of forms if else if else if else > but was > wondering if

Re: [R] if else elseif for data frames

2012-08-12 Thread David Winsemius
On Aug 12, 2012, at 5:43 PM, Sachinthaka Abeywardana wrote: Hi all, It seems like I cannot use normal 'if' for data frames. What would be the best way to do the following. if data$col1='high' data$col2='H' else if data$col1='Neutral' data$col2='N' else if data$col='low' data$col2='

Re: [R] if else elseif for data frames

2012-08-12 Thread Jeff Newmiller
Maybe the cut function? ?cut --- Jeff NewmillerThe . . Go Live... DCN:Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Pl

Re: [R] if else elseif for data frames

2012-08-12 Thread jim holtman
You might look at the 'recode' that is part of the 'car' package. You can also setup a dataframe with two columns; one with the current value and one with the new value you want, then you can use 'merge' or 'match' to create your new column. If you have provided some sample data, I could have pro

Re: [R] if else elseif for data frames

2012-08-12 Thread arun
Hi, Try this: dat1<-data.frame(col1=c(rep("high",3),rep("Neutral",3),rep("low",4)))  dat1$col2<-ifelse(dat1$col1=="high",dat1$col2<-"H",ifelse(dat1$col1=="Neutral",dat1$col2<-"N","L")) dat1   col1 col2 1 high    H 2 high    H 3 high    H 4  Neutral    N 5  Neutral    N 6  Neutral   

Re: [R] if else elseif for data frames

2012-08-12 Thread Sachinthaka Abeywardana
The thing is I have about 10 cases. I saw the ifelse statement but was wondering if there was a cleaner method of doing it. The coding will get really messy when I write all 10 cases. Cheers, Sachin On Mon, Aug 13, 2012 at 11:04 AM, arun wrote: > Hi, > Try this: > dat1<-data.frame(col1=c(rep("h

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

2012-01-13 Thread Joshua Wiley
Hi jost87, Try using a good text editor with syntax highlighting and parenthesis/brace matching (personal preference is Emacs + ESS, others like Rstudio, and there are tons more). I find at least one mismatched pair of parentheses/braces in those expressions. Cheers, Josh On Fri, Jan 13, 2012

Re: [R] if/else scope

2011-11-15 Thread P.B. Lecavalier
On 11/15/2011 06:46 PM, Kevin Burton wrote: What is wrong with the following? x<- 1:2 if(x[1]> 0) { if(x[2]> 0) { print("1& 2> 0") } else {

Re: [R] if/else scope

2011-11-15 Thread Ben Tupper
Hi, Check out this R-help thread from 2007. http://tolstoy.newcastle.edu.au/R/e2/help/07/06/19513.html Cheers, Ben On Nov 15, 2011, at 6:46 PM, Kevin Burton wrote: > What is wrong with the following? > > > > x <- 1:2 > > if(x[1] > 0) > > { > >if(x[2] > 0) > >

Re: [R] if/else scope

2011-11-15 Thread Peter Langfelder
On Tue, Nov 15, 2011 at 3:46 PM, Kevin Burton wrote: > What is wrong with the following? AFAIK the else needs to follow the end brace of if {} on the same line, at least at the main level. Peter > > > > x <- 1:2 > > if(x[1] > 0) > > { > >                if(x[2] > 0) > >                { > >  

Re: [R] if else lop

2011-07-05 Thread Bert Gunter
Hi Peter: Beware that "gnashing of teeth" business. My Dad, a dentist, said that a couple of his patients who were musicians did this when they played and ground their teeth down so much he had to make them dentures! ;-) Cheers, Bert On Tue, Jul 5, 2011 at 2:53 PM, peter dalgaard wrote: > > On

Re: [R] if else lop

2011-07-05 Thread Joshua Wiley
On Tue, Jul 5, 2011 at 2:53 PM, peter dalgaard wrote: > > On Jul 5, 2011, at 22:27 , gary engstrom wrote: > >> I am trying to use if...else loop > > Argh! > > A loop goes _around_ and around. "for", "repeat", "while". > > "if ... else"  is a _branching_ construct. Nominated for a fortune. > > -

Re: [R] if else lop

2011-07-05 Thread peter dalgaard
On Jul 5, 2011, at 22:27 , gary engstrom wrote: > I am trying to use if...else loop Argh! A loop goes _around_ and around. "for", "repeat", "while". "if ... else" is a _branching_ construct. -- Peter Dalgaard Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederi

Re: [R] if else lop

2011-07-05 Thread Sarah Goslee
Gary, Was the second half of my message this morning not clear enough? It wasn't clear from your original message that you were using isin() from the prob package, rather than using isin() as pseudocode, so I'd written a function to do that part. But the second half of my message went through the

Re: [R] if else loop

2011-07-05 Thread Sarah Goslee
Hi Gary, A solution in two pieces. First, you need to be able to match the rows of your data frame. There might be a more elegant way to do it, but I couldn't think of one that gave the option of ordering or not, so I wrote a function: isin <- function(dd, tomatch, ordered=TRUE) { # find toma

Re: [R] if else statements in data frame

2011-04-17 Thread ozgrerg
Thanks alot B77S. That was a critical post. -- View this message in context: http://r.789695.n4.nabble.com/if-else-statements-in-data-frame-tp3454646p3455077.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailin

Re: [R] if else statements in data frame

2011-04-16 Thread ozgrerg
Thanks for the answers. They were very helpful. I did it like below. data$z <- ifelse(data$x == "A" & data$y == "A", "NA", "B"). That is enough for today. I guess I learned a enough today. Thanks to everbody who tried to help. -- View this message in context: http://r.789695.n4.nabble.com/i

Re: [R] if else statements in data frame

2011-04-16 Thread helin_susam
Hi ozgrerg, I am not a "good" R user, but according to me you can do this; data <- data.frame(x=c("A","A","B","A"), y=c("A","B","B","B")) for (i in 1:nrow(data)){ if(data$x[i]=="B"||data$y[i]=="B") z[i]=c("B") else z[i]=c("NA") } cbind(data,z) -- View this message in context: http://r.789695.n

  1   2   >