similar to: question about if else

Displaying 20 results from an estimated 20000 matches similar to: "question about if else"

2004 Feb 27
4
question
Hi everybody. The question: I get two vectors 'iFalseFalse' and 'i2'. I think they should be the same but they are not. Is it because R does not handle complicated logical expressions in such cases or I do something wrong? > z1 = c(NA, "", 3, NA, "", 3) > z2 = c("", "", 3, NA, 3, NA) > cV = (as.character(z1)==as.character(z2))
2004 Feb 24
1
bug in ifelse (PR#6611)
> version _ platform i386-pc-linux-gnu arch i386 os linux-gnu system i386, linux-gnu status major 1 minor 8.1 year 2003 month 11 day 21 language R > aa <- as.POSIXct(c('1/1/1980','1/2/1980','')) > ifelse(is.na(aa),as.POSIXct('1/3/1980'),aa) Error in rep.int(unclass(x), times) : Argument "times" is missing,
2004 Feb 18
6
interesting feature
Hi, everybody. This was an interesting discussion last time and it helped me a lot. Could you please have a look at some feature and tell me why it was designed this way (my questions are under #########) > x = c(1, 10) > y = c(99, 55) > d <- data.frame(x = x, y = y) > d x y 1 1 99 2 10 55 > add <- data.frame(x = 14, y = 99) > add x y 1 14 99 > d <-
2004 Feb 05
1
What is the correct way of using function C() for factors:
The funciton c() works differently for strings and for factors: For strings: > l = c('a', 'b') > l [1] "a" "b" For factors: > l = c(factor('a'), factor('b')) > l [1] 1 1 What should be the right technique for merging factors? -- Svetlana Eden Biostatistician II School of Medicine
2004 Feb 27
1
question about setdiff()
Thank you for your answers, I have another question: the behaviour of setdiff(indicesFalse, indicesNA) does not seem predictable to me. > indices [1] 1 2 3 4 5 6 > compareVector [1] NA TRUE TRUE TRUE FALSE NA > indicesNA = indices[is.na(compareVector)] > indicesNA [1] 1 6 > indicesFalse = indices[compareVector == FALSE] > indicesFalse [1] NA 5 NA >
2004 Feb 05
2
correction to the previously asked question (about merging factors)
I have two factors l1, l2, and I'd like to merge them. (Remark: The factors can not be converted to charaters) Function c() does not give me the result I want: > l1 = factor(c('aaaa', 'bbbb')) > l2 = factor(c('ccc', 'dd')) > lMerge = factor(c(l1, l2)) > lMerge [1] 1 2 1 2 Levels: 1 2 > I'd like to merge l1 and l2 and to get lMerge
2004 Mar 26
1
lookup.xport in foreign ignoring some datasets (PR#6701)
The Details. In the following version. > version _ platform i386-pc-linux-gnu arch i386 os linux-gnu system i386, linux-gnu status major 1 minor 8.1 year 2003 month 11 day 21 language R > lookup.xport ignores some datasets in sas export file. File "emptySasData3.xpt" (available at http://biostat.mc.vanderbilt.edu/tmp/emptySasData3.xpt) is a
2017 Dec 13
4
difference between ifelse and if...else?
Hi there, I don't know why the following codes are return different results. > ifelse(3 > 2, 1:3, length(1:3)) [1] 1 > if (3 > 2) 1:3 else length(1:3) [1] 1 2 3 Any hints? Best, Jinsong
2009 Jun 08
5
if else
Hi R-helpers! I have the following dataframe: firm<-c(rep(1:3,4)) year<-c(rep(2001:2003,4)) X1<-rep(c(10,NA),6) X2<-rep(c(5,NA,2),4) data<-data.frame(firm, year,X1,X2) data So I want to obtain the same dataframe with a variable X3 that is: X1, if X2=NA X2, if X1=NA X1+X2 if X1 and X2 are not NA So my final data is X3<-c(15,NA,12,5,10,2,15,NA,12,5,10,2)
2018 Apr 30
3
How to visualise what code is processed within a for loop
Luca, If speed is important, you might improve performance by making d0 into a true matrix, rather than a data frame (assuming d0 is indeed a data frame at this point). Although data frames may look like matrices, they aren?t, and they have some overhead that matrices don?t. I don?t think you would be able to use the [[nm]] syntax with a matrix, but [ , nm] should work, provided the matrix has
2018 Apr 28
2
How to visualise what code is processed within a for loop
I forgot to explain why my suggestion. The logical condition returns FALSE/TRUE that in R are coded as 0/1. So all you have to do is coerce to integer. This works because the ifelse will return a 1 or a 0 depending on the condition. Meaning exactly the same values. And is more efficient since ifelse creates both vectors, the true part and the false part, and then indexes those vectors in
2018 Apr 28
2
How to visualise what code is processed within a for loop
Thanks Don, for (i in 1:10){ nm <- paste0("V", i) d0[[nm]] <- ifelse( regexpr(d1[i,1], d0$X0) > 0, 1, 0) } is exaclty what I needed. Best regards, Luca 2018-04-25 23:03 GMT+02:00 MacQueen, Don <macqueen1 at llnl.gov>: > Your code doesn't make sense to me in a couple of ways. > > Inside the loop, the first line assigns a value to an
2018 Apr 30
0
How to visualise what code is processed within a for loop
Hi Rui Thank you for your suggestion, I have tested the code suggested by you against that supplied by Don in terms of timing and results are very much aligned: to populate a 5954x899 0/1 matrix on my machine your procedure took 79 secs, while the one with ifelse employed 80 secs, hence unfortunately not really any significant time saved there. Nevertheless thank you for your contribution.
2008 Oct 03
1
Error message in ifthen else
Hi, I came across the below error when I try to do ifelse condition:- Error in "[<-"(`*tmp*`, test, value = rep(yes, length = length(ans))[test]) : incompatible types What I am trying to accomplish is :- for(x in 1:ncol(filterpred)){ sumno<-sum(filterpred[no,x]) sumyes<-sum(filterpred[yes,x]) ifelse(sumno==0 && sumyes !=0,
2018 Apr 30
0
How to visualise what code is processed within a for loop
Thank you for both replies Don & Rui, The very issue here is that there is a search that needs to be done within a text field and I agree with Rui later comment that regexpr might indeed be the time consuming piece of code. I might try to optimise this piece of code later on, but for the time being I am working on the following part of building a neural network to try indeed classifying some
2018 Apr 25
0
How to visualise what code is processed within a for loop
Your code doesn't make sense to me in a couple of ways. Inside the loop, the first line assigns a value to an object named "t". Then, the second line does the same thing, assigns a value to an object named "t". The value of the object named "t" after the second line will be the output of the ifelse() expression, whatever that is. This has the effect of making
2018 Apr 28
0
How to visualise what code is processed within a for loop
Hello, instead of ifelse, the following is exactly the same and much more efficient. d0[[nm]] <- as.integer(regexpr(d1[i,1], d0$X0) > 0) Hope this helps, Rui Barradas On 4/28/2018 8:45 PM, Luca Meyer wrote: > Thanks Don, > > for (i in 1:10){ > nm <- paste0("V", i) > d0[[nm]] <- ifelse( regexpr(d1[i,1], d0$X0) > 0, 1, 0) > }
2018 Apr 24
4
How to visualise what code is processed within a for loop
Hi, I am trying to debug the following code: for (i in 1:10){ t <- paste("d0$V",i,sep="") t <- ifelse(regexpr(d1[i,1],d0$X0)>0,1,0) } and I would like to see what code is actually processing R, how can I do that? More to the point, I am trying to update my variables d0$V1 to d0$V10 according to the presence or absence of some text (contained in the file d1)
2007 Sep 04
2
For loop with if else statement
Hi, I try to make a simple for loop with a if else statement (First example - Below) and extend it to a more complex loop (Second example). However, my results #First example: x=c(1,2) t=for(i in 1:length(x)){ if (x==1){a=x+1}else if (x==2){a=x} } Returned from R: Warning messages: 1: the condition has length > 1 and only the first element will be used in: if (x == 1) { 2: the condition has
2005 Jun 01
2
A suggestion to improve ifelse behaviour with vector yes/noarguments
> Thomas Lumley wrote: > > On Tue, 31 May 2005, Duncan Murdoch wrote: > > > > > >>M??kinen Jussi wrote: > >> > >>>Dear All, > >>> > >>>I luckily found the following feature (or problem) when tried to > >>>apply > >>>ifelse-function to an ordered data. > >>> > >>> >