Re: [R] if statement and for loop question

2021-06-01 Thread Michael Dewey
Dear Kai When you ask again it is best to tell us what your input is and what output you were hoping for and what you actually got. If you can make a small data-set which shows all that then your post will be much more likely to get a helpful response. If you want to transfer the data-set to

Re: [R] if statement and for loop question

2021-05-31 Thread Kai Yang via R-help
Hi Jim, Sorry to post "same" question, because  1. I was asking to use plain text format. I have to post my question again. But I don't know if it is working. 2. I'm a beginner for R (< 2 month). It may not easy for me to ask a "clear" R question. My current work is to transfer my SAS code into

Re: [R] if statement and for loop question

2021-05-30 Thread Jim Lemon
Hi Kai, You seem to be asking the same question again and again. This does not give us the warm feeling that you know what you want. testdf<-data.frame(a=c("Negative","Positive","Neutral","Random","VUS"), b=c("No","Yes","No","Maybe","Yes"), c=c("Off","On","Off","Off","On"), d=c("Bad","Good","Ba

Re: [R] if statement and for loop question

2021-05-30 Thread Rui Barradas
Hello, You don't need a loop, the R way is a vectorized solution and it's also clearer. Create a logical index (note only one &) and assign b, c, d where it's TRUE. i <- try$a != "Positive" & try$a != "VUS" try <- within(try, { b[i] <- '' c[i] <- '' d[i] <- '' }) Hope this helps, Rui

Re: [R] if statement and for loop question

2021-05-30 Thread Berry, Charles
Kai, You have made a simple mistake. And now you cannot see it. I believe this is not uncommon among programmers. It has happened to me more times than I want to recall. > On May 30, 2021, at 9:28 AM, Kai Yang via R-help wrote: > > Hello List,I have a data frame which having the character c

Re: [R] if statement and for loop question

2021-05-30 Thread Jeff Newmiller
Can you make R code that creates an actual sample data frame that looks like you want the answer to look? say, just using the data.frame function and literal strings. Oh, and read the Posting Guide... you need to send your email using plain text format or it may get garbled when the list strips

[R] if statement and for loop question

2021-05-30 Thread Kai Yang via R-help
Hello List,I have a data frame which having the character columns: | a1 | b1 | c1 | d1 | | a2 | b2 | c2 | d2 | | a3 | b3 | c3 | d3 | | a4 | b4 | c4 | d4 | | a5 | b5 | c5 | d5 | I need to do: if a1 not = "Positive" and not = "VUS" then values of  b1, c1 and d1 will be zero out. And do the same

Re: [R] If statement

2019-09-12 Thread Jeff Newmiller
Use ifelse function, not if. If is only good for one logical value at a time, but you are working with long vectors of values simultaneously. I have no interest in doing all of your workfor you, but the concept is cpl_or_sngl <- dat1$b %in% c( "couple", "single" ) a_pvt <- "private" == dat1$a da

Re: [R] If statement

2019-09-12 Thread Bert Gunter
You appear to be confusing && with & and || with | ; (the first of each pair take a logical expression, the second of each a logical vector) ... as well as if ... else with ifelse (the first is a flow control statement taking a logical expression; the second is a function taking a logical vector

[R] If statement

2019-09-12 Thread Val
Hi all, I am trying to use the if else statement and create two new columns based on the existing two columns. Below please find my sample data, dat1 <-read.table(text="ID a b c d A private couple 25 35 B private single 24 38 C none single28 32 E none none 20 36 ",header=TRUE,stringsAs

Re: [R] If statement not working in a for loop when making it independent

2014-09-29 Thread Jeff Newmiller
You have really tied yourself up in a knot here. Last I checked, when A==B, then B==A. You also need to study the difference between ?if and ?ifelse, since you are not giving the if function the scalar it expects. For example, i$ID is a vector of three (identical, due to your use of split) value

[R] If statement not working in a for loop when making it independent

2014-09-29 Thread Nebulo Archae
Dear all, I have a data.frame xy that contains numeric data and data_qual which contains qualitative data which I want to include in a for loop with an if statement (commented out in the code below). The if statement should be applied if the ID in data_qual$ID is the same than in xy$ID. I am t

Re: [R] If-statement in for-loop

2013-12-11 Thread Gerrit Eichner
Hello, "gncl dzgn", your problem has a flavor of homework which is usually not delt with on this list. However, a few comments: 0. The description of your problem is rather vague, in particular, the meaning of "input" in the description of your "conditions" is unclear! (By the way, your main

[R] If-statement in for-loop

2013-12-10 Thread gncl dzgn
Hi everyone, you might find my question elementary but I am a beginner and unfortunately I can't fix the problem. So, I simulate this following algorithm and some values of c are NA. Therefore, I should add these following two if-statements but I don't know how I should do it in a for-loop.

Re: [R] If statement - copying a factor variable to a new variable

2012-06-28 Thread peter dalgaard
On Jun 28, 2012, at 09:42 , Rui Barradas wrote: > Hello, > > Another way is to use index vectors: > > > v1.factor <- c("S","S","D","D","D",NA) > v2.factor <- c("D","D","S","S","S","S") > > td2 <- test.data <- data.frame(v1.factor,v2.factor) > > for (i in 1:nrow(test.data) ) { > >[... et

Re: [R] If statement - copying a factor variable to a new variable

2012-06-28 Thread Miguel Manese
On Thu, Jun 28, 2012 at 8:47 PM, James Holland wrote: > With the multiple if statements I need to check for, I though for statements > with the if/else if conditional statement was better than nested ifelse > functions. for () gives you a lot of flexibility at the expense of being verbose & slow,

Re: [R] If statement - copying a factor variable to a new variable

2012-06-28 Thread James Holland
Yeah, the reason I didn't use ifelse is because I've got multiple variables to manipulate based on the if statement, some factors and some numeric. I have to look at the factor variables, and based on that, either use one series of variables or another. With the multiple if statements I need to c

Re: [R] If statement - copying a factor variable to a new variable

2012-06-28 Thread Rui Barradas
Hello, Another way is to use index vectors: v1.factor <- c("S","S","D","D","D",NA) v2.factor <- c("D","D","S","S","S","S") td2 <- test.data <- data.frame(v1.factor,v2.factor) for (i in 1:nrow(test.data) ) { [... etc ...] } #End FOR # Create index vectors na1 <- is.na(v1.factor) na2 <-

Re: [R] If statement - copying a factor variable to a new variable

2012-06-28 Thread Miguel Manese
Hi James, On Thu, Jun 28, 2012 at 12:33 AM, James Holland wrote: > I need to look through a dataset with two factor variables, and depending > on certain criteria, create a new variable containing the data from one of > those other variables. > > The problem is, R keeps making my new variable an

[R] If statement - copying a factor variable to a new variable

2012-06-27 Thread James Holland
I need to look through a dataset with two factor variables, and depending on certain criteria, create a new variable containing the data from one of those other variables. The problem is, R keeps making my new variable an integer and saving the data as a 1 or 2 (I believe the levels of the factor)

Re: [R] if statement

2012-03-07 Thread Val
Now I got results as I wanted. Thank you all. On Wed, Mar 7, 2012 at 2:51 PM, AAsk wrote: > > x <- -1:4 > > x<0 # returns TRUE (1) or FALSE (0) > [1] TRUE FALSE FALSE FALSE FALSE FALSE > > x+as.numeric(x<0) > [1] 0 0 1 2 3 4 > > __ > R-help@r-project.

Re: [R] if statement

2012-03-07 Thread AAsk
> x <- -1:4 > x<0 # returns TRUE (1) or FALSE (0) [1] TRUE FALSE FALSE FALSE FALSE FALSE > x+as.numeric(x<0) [1] 0 0 1 2 3 4 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-

Re: [R] if statement

2012-03-07 Thread Ted Harding
The simplest method would be: x[x<0] <- x[x<0]+1 x <- -1:4 x # [1] -1 0 1 2 3 4 x[x<0] <- x[x<0]+1 x # [1] 0 0 1 2 3 4 I think where Val got confused is in thinking that if(x<0) is applied separately to each element of x, one at a time. What actually happens, of course, i

Re: [R] if statement

2012-03-07 Thread Jorge I Velez
Try > ifelse( x < 0, x + 1, x) [1] 0 0 1 2 3 4 See also ?ifelse. HTH, Jorge.- On Wed, Mar 7, 2012 at 2:12 PM, Val <> wrote: > Hi All, > > I have one difficulty in using the conditional if statement > > Assume , > > x <- -1:4 > x > [1] -1 0 1 2 3 4 > > if x is lees than want I want to ad

Re: [R] if statement

2012-03-07 Thread Sarah Goslee
You need ifelse() instead of if(). On Wed, Mar 7, 2012 at 2:12 PM, Val wrote: > Hi All, > > I have one difficulty in using the conditional if statement > > Assume , > > x <- -1:4 >  x > [1] -1  0  1  2  3  4 > > if x is lees than want I want to add 1 and I used the following command >  if(x<0) {x

[R] if statement

2012-03-07 Thread Val
Hi All, I have one difficulty in using the conditional if statement Assume , x <- -1:4 x [1] -1 0 1 2 3 4 if x is lees than want I want to add 1 and I used the following command if(x<0) {x=x+1} Warning message: In if (x < 0) { : the condition has length > 1 and only the first element

Re: [R] if statement problem

2011-12-25 Thread Rui Barradas
Hello again. I don't understand what didn't work. First, it seems better to use 'nrow', the result is the same stopifnot(length(x[,1]) == nrow(x)) Then your multiple OR condition. #if((x[i,1] || x[i,2] || x[i,3] || x[i,4]) < 5) x <- matrix(1:24, ncol=4) for(i in 1:nrow(x)) if(any(x

Re: [R] if statement problem

2011-12-25 Thread Uwe Ligges
On 24.12.2011 12:03, reena wrote: It didn't work. :( What did not work??? Please do not misuse the R-help mailing list! Its posting guide clearly asks you to cite the thread and specify reproducible examples that make other able to help. Best, Uwe Ligges -- View this message in cont

Re: [R] if statement problem

2011-12-24 Thread reena
It didn't work. :( -- View this message in context: http://r.789695.n4.nabble.com/if-statement-problem-tp4230026p4230933.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/lis

Re: [R] if statement problem

2011-12-24 Thread Patrick Burns
This is almost Circle 8.1.7 of 'The R Inferno': http://www.burns-stat.com/pages/Tutor/R_inferno.pdf but is making the mistake in the other direction. On 23/12/2011 22:40, reena wrote: Hello, I want to do fisher test for the rows in data file which has value less than 5 otherwise chi square te

Re: [R] if statement problem

2011-12-23 Thread Rui Barradas
reena wrote > > Hello, > > I want to do fisher test for the rows in data file which has value less > than 5 otherwise chi square test .The p values from both test should be > stored in one resulted file. but there is some problem with bold if > statement. I don't know how > implement this line

[R] if statement problem

2011-12-23 Thread reena
Hello, I want to do fisher test for the rows in data file which has value less than 5 otherwise chi square test .The p values from both test should be stored in one resulted file. but there is some problem with bold if statement. I don't know how implement this line properly. x = cbind(obs1,obs

Re: [R] If Statement

2011-03-08 Thread William Dunlap
> -Original Message- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of dpender > Sent: Tuesday, March 08, 2011 4:07 AM > To: r-help@r-project.org > Subject: [R] If Statement > > Hi, > > I am having some problems us

Re: [R] If Statement

2011-03-08 Thread Petr Savicky
On Tue, Mar 08, 2011 at 04:07:03AM -0800, dpender wrote: > Hi, > > I am having some problems using the if statement correctly. I have used it > many times previously so I dona't know what is different with this case. > Here is my problem: > > I have a 1X10 matrix of values as follows: > > > H.

Re: [R] If Statement

2011-03-08 Thread David Winsemius
On Mar 8, 2011, at 7:07 AM, dpender wrote: Hi, I am having some problems using the if statement correctly. I have used it many times previously so I dona't know what is different with this case. Here is my problem: I have a 1X10 matrix of values as follows: H.MC [,1] [1,] 4.

Re: [R] If Statement

2011-03-08 Thread andrija djurovic
Hi, matrix has a 2 dimensions. Is this work: a<-matrix(rep(c(1,2),c(5,5)),ncol=1) dim(a) for (i in 1:10) { ifelse(a[i,]==1, a[i,]<-runif(1,3,4.5), a[i,]) } a Andrija On Tue, Mar 8, 2011 at 1:07 PM, dpender wrote: > Hi, > > I am having some problems using the if statement correctly. I have u

[R] If Statement

2011-03-08 Thread dpender
Hi, I am having some problems using the if statement correctly. I have used it many times previously so I dona't know what is different with this case. Here is my problem: I have a 1X10 matrix of values as follows: > H.MC [,1] [1,] 4.257669 [2,] 7.023242 [3,] 4.949857 [4,] 5.1070

Re: [R] if statement and truncated distribution

2010-10-25 Thread Nick Sabbe
lly Luo Sent: maandag 25 oktober 2010 2:01 To: r-help@r-project.org Subject: [R] if statement and truncated distribution Hi R helpers, I am trying to use the if statement to generate a truncated random variable as follows: if (y[i]==0) { v[i] ~ rnorm(1,0,1) | (-inf ,0) } if (y[i]==1) { v[i] ~ rnorm

[R] if statement and truncated distribution

2010-10-24 Thread Sally Luo
Hi R helpers, I am trying to use the if statement to generate a truncated random variable as follows: if (y[i]==0) { v[i] ~ rnorm(1,0,1) | (-inf ,0) } if (y[i]==1) { v[i] ~ rnorm(1,0,1) | (0, inf) } I guess I cannot use " | ( , ) " to restrict the range of a variable in R. Could you let me kno

Re: [R] If Statement Help

2010-10-23 Thread David Winsemius
On Oct 23, 2010, at 7:44 PM, Jason Kwok wrote: Thanks Jorge. It works. Is there a way to keep the actual price in the price column instead of TRUE/FALSE but filtering on when price>100? Huh? When I use subset I get what you ask for: > subset(x, Price > 100) Price 2010-10-12 10

Re: [R] If Statement Help

2010-10-23 Thread Jason Kwok
Thanks Jorge. It works. Is there a way to keep the actual price in the price column instead of TRUE/FALSE but filtering on when price>100? Thanks, Jay On Sat, Oct 23, 2010 at 10:37 PM, Jorge Ivan Velez wrote: > Hi Jay, > > If "x" is your data, you could use subset() to do what you want: > >

Re: [R] If Statement Help

2010-10-23 Thread Jason Kwok
Thanks for the help Jim. As a new user and member of this mailing list, I'm very impressed with all the support! Jay On Sat, Oct 23, 2010 at 10:21 PM, jim holtman wrote: > Need to understand how 'indexing' is done in R: > > > x <- read.table(textConnection(" Price > + 2010-10

Re: [R] If Statement Help

2010-10-23 Thread Jorge Ivan Velez
Hi Jay, If "x" is your data, you could use subset() to do what you want: subset(x, Price > 100) See ?subset for more information. HTH, Jorge On Sat, Oct 23, 2010 at 9:56 PM, Jason Kwok <> wrote: >Price > 2010-10-11 99 > 2010-10-12101 > 2010-10-13102 > 2010-10-

Re: [R] If Statement Help

2010-10-23 Thread jim holtman
Need to understand how 'indexing' is done in R: > x <- read.table(textConnection(" Price + 2010-10-11 99 + 2010-10-12101 + 2010-10-13102 + 2010-10-14103 + 2010-10-15 99 + 2010-10-18 98 + 2010-10-19 97 + 2010-10-20101 + 2010-10-21101 + 2010-10-2

[R] If Statement Help

2010-10-23 Thread Jason Kwok
Price 2010-10-11 99 2010-10-12101 2010-10-13102 2010-10-14103 2010-10-15 99 2010-10-18 98 2010-10-19 97 2010-10-20101 2010-10-21101 2010-10-22101 I have this dataset and I only want to return instances when the Price is > 100. If I use t

Re: [R] If Statement with more than one condition

2010-10-22 Thread news
"Santosh Srinivas" writes: > I'm unable to find the OR operator like other language .. any suggestions? > > I want to do If (condition1 OR condition 2){ do something } > if((condition1) | (condition2)){ do something } -- aleblanc __ R-help@r-project

Re: [R] If Statement with more than one condition

2010-10-22 Thread Duncan Murdoch
Joshua Wiley wrote: Hi Santosh, I believe you are looking for |. For example: if(3 > 5 | 3 < 4) {print(TRUE)} In an if () statement you use || more often. | is a vector operator that always evaluates both arguments; || is a scalar operator that quits if the left hand argument determines t

Re: [R] If Statement with more than one condition

2010-10-22 Thread Joshua Wiley
Hi Santosh, I believe you are looking for |. For example: if(3 > 5 | 3 < 4) {print(TRUE)} Cheers, Josh On Fri, Oct 22, 2010 at 12:51 AM, Santosh Srinivas wrote: > I'm unable to  find the OR operator like other language .. any suggestions? > > I want to do If (condition1 OR condition 2){ do s

[R] If Statement with more than one condition

2010-10-22 Thread Santosh Srinivas
I'm unable to find the OR operator like other language .. any suggestions? I want to do If (condition1 OR condition 2){ do something } Thanks for answering this elementary question. [[alternative HTML version deleted]] __ R-help@r-proj

Re: [R] If statement generates two outputs

2009-03-24 Thread Wacek Kusnierczyk
Thomas Lumley wrote: > On Tue, 24 Mar 2009, Wacek Kusnierczyk wrote: > >> >> thanks for the explanation. for the sake of rvalues, please do not fix >> this bug. i find it useful to avoid messing with = and <-. >> > > If you insist on calling it a bug, the chances of its getting fixed > are higher

Re: [R] If statement generates two outputs

2009-03-24 Thread Thomas Lumley
On Tue, 24 Mar 2009, Wacek Kusnierczyk wrote: thanks for the explanation. for the sake of rvalues, please do not fix this bug. i find it useful to avoid messing with = and <-. If you insist on calling it a bug, the chances of its getting fixed are higher. -thomas Thomas Lumley

Re: [R] If statement generates two outputs

2009-03-24 Thread Wacek Kusnierczyk
Thomas Lumley wrote: > On Tue, 24 Mar 2009, Wacek Kusnierczyk wrote: > >> thanks. so it seems to be intentionally parsable, though i wouldn't say >> that this gives a meaning to ':=' -- the operator has a syntactic >> category, but no semantics. the syntactic category does not imply any >> semant

Re: [R] If statement generates two outputs

2009-03-24 Thread Thomas Lumley
On Tue, 24 Mar 2009, Wacek Kusnierczyk wrote: thanks. so it seems to be intentionally parsable, though i wouldn't say that this gives a meaning to ':=' -- the operator has a syntactic category, but no semantics. the syntactic category does not imply any semantics, as in '<-' = function(a,

Re: [R] If statement generates two outputs

2009-03-24 Thread Wacek Kusnierczyk
Romain Francois wrote: > This is probably due to that in the gram.y file : > > case ':': >if (nextchar(':')) { >if (nextchar(':')) { >yylval = install(":::"); >return NS_GET_INT; >} >else { >yylval = install("::"); >return NS_GET; >

Re: [R] If statement generates two outputs

2009-03-24 Thread Romain Francois
Wacek Kusnierczyk wrote: Berwin A Turlach wrote: G'day Carl, On Mon, 23 Mar 2009 20:11:19 -0400 Carl Witthoft wrote: But seriously: can someone explain to me what's going on in the rvalues.r code? I tried a simple experiment, replacing ":=" with a "colec" in the code, and of c

Re: [R] If statement generates two outputs

2009-03-24 Thread Wacek Kusnierczyk
Berwin A Turlach wrote: > G'day Carl, > > On Mon, 23 Mar 2009 20:11:19 -0400 > Carl Witthoft wrote: > > > >> But seriously: can someone explain to me what's going on in the >> rvalues.r code? I tried a simple experiment, replacing ":=" with a >> "colec" in the code, and of course the line >

Re: [R] If statement generates two outputs

2009-03-24 Thread Wacek Kusnierczyk
Berwin A Turlach wrote: > G'day Carl, > > On Mon, 23 Mar 2009 20:11:19 -0400 > Carl Witthoft wrote: > > >> >From: Wacek Kusnierczyk >> >Date: Sun, 22 Mar 2009 22:58:49 +0100 >> >> >> >just for fun, you could do this with multiassignment, e.g., using >> >the (highly experimental and prematu

Re: [R] If statement generates two outputs

2009-03-24 Thread Wacek Kusnierczyk
Berwin A Turlach wrote: > G'day Carl, > > On Mon, 23 Mar 2009 20:11:19 -0400 > Carl Witthoft wrote: > > >> >From: Wacek Kusnierczyk >> >Date: Sun, 22 Mar 2009 22:58:49 +0100 >> >> >> >just for fun, you could do this with multiassignment, e.g., using >> >the (highly experimental and prematu

Re: [R] If statement generates two outputs

2009-03-23 Thread Berwin A Turlach
G'day Carl, On Mon, 23 Mar 2009 20:11:19 -0400 Carl Witthoft wrote: > >From: Wacek Kusnierczyk > >Date: Sun, 22 Mar 2009 22:58:49 +0100 > > > >just for fun, you could do this with multiassignment, e.g., using > >the (highly experimental and premature!) rvalues: > > >source('http://m

Re: [R] If statement generates two outputs

2009-03-23 Thread Carl Witthoft
>From: Wacek Kusnierczyk >Date: Sun, 22 Mar 2009 22:58:49 +0100 >just for fun, you could do this with multiassignment, e.g., using the >(highly experimental and premature!) rvalues: >source('http://miscell.googlecode.com/svn/rvalues/rvalues.r') >if (TRUE) > c(df1, df2) := list(4

Re: [R] If statement generates two outputs

2009-03-23 Thread Wacek Kusnierczyk
jimdare wrote: > Hi, > > I tried to create the following if / else statement but I keep getting the > error "Error: unexpected '}' in "size="large",center="none") > }" (I have highlighted the } in bold where the error is occuring). I can't > seem to find a reason for this, does anyone know how I

Re: [R] If statement generates two outputs

2009-03-22 Thread jim holtman
You appear to have a number of unbalanced parentheses in your statements. Here is one that seems to work: if (nostocks<=3) {data1<-test[,data1stocks]; tex1<-latex(data1, file=paste(i$Species[1], "1.tex", sep=""), rowname = NULL,

Re: [R] If statement generates two outputs

2009-03-22 Thread jimdare
Hi, I tried to create the following if / else statement but I keep getting the error "Error: unexpected '}' in "size="large",center="none") }" (I have highlighted the } in bold where the error is occuring). I can't seem to find a reason for this, does anyone know how I can fix it? Thanks, Jame

Re: [R] If statement generates two outputs

2009-03-22 Thread Wacek Kusnierczyk
jimdare wrote: > Thanks very much > > you're welcome. just for fun, you could do this with multiassignment, e.g., using the (highly experimental and premature!) rvalues: source('http://miscell.googlecode.com/svn/rvalues/rvalues.r') if (TRUE) c(df1, df2) := list(4:8, 9:13)

Re: [R] If statement generates two outputs

2009-03-22 Thread jimdare
Thanks very much Wacek Kusnierczyk wrote: > > jimdare wrote: >> Hi, >> >> How do I tell an if statement to generate two seperate outputs. >> >> E.g If X>5 I want to create df1 and df2: >> >> if (X>5) {df1<-c(4,5,6,7,8) AND df2<-c(9,10,11,12,13)} >> > almost there: > > if (X>5) {df1<-

Re: [R] If statement generates two outputs

2009-03-22 Thread Daniel Moreira
if (X>5) { df1<-c(4,5,6,7,8) df2<-c(9,10,11,12,13) } hth, Daniel Moreira, MD jimdare Sent by: r-help-boun...@r-project.org 03/22/2009 05:27 PM To r-help@r-project.org cc Subject [R] If statement generates two outputs Hi, How do I tell an if statement to generate two

Re: [R] If statement generates two outputs

2009-03-22 Thread Wacek Kusnierczyk
jimdare wrote: > Hi, > > How do I tell an if statement to generate two seperate outputs. > > E.g If X>5 I want to create df1 and df2: > > if (X>5) {df1<-c(4,5,6,7,8) AND df2<-c(9,10,11,12,13)} > almost there: if (X>5) {df1<-c(4,5,6,7,8); df2<-c(9,10,11,12,13)} vQ

[R] If statement generates two outputs

2009-03-22 Thread jimdare
Hi, How do I tell an if statement to generate two seperate outputs. E.g If X>5 I want to create df1 and df2: if (X>5) {df1<-c(4,5,6,7,8) AND df2<-c(9,10,11,12,13)} Thanks, James -- View this message in context: http://www.nabble.com/If-statement-generates-two-outputs-tp22650844p22650844.htm

Re: [R] if statement of a vector

2009-02-23 Thread Jorge Ivan Velez
Hi there, Try this: x<-your.vector x[x>10] HTH, Jorge On Mon, Feb 23, 2009 at 12:31 PM, bioinformatics_guy wrote: > > right now I have a vector of about 1000 points. I'd like to iterate through > each of these points and and test if it is greater than a certain value and > if not, throw it o

[R] if statement of a vector

2009-02-23 Thread bioinformatics_guy
right now I have a vector of about 1000 points. I'd like to iterate through each of these points and and test if it is greater than a certain value and if not, throw it out. x=vector y=empty vector j=0 for i (0..length[vector]) if x[i] > 10 y[j] = x[i] j++ Thats basical

Re: [R] if statement

2009-01-05 Thread Wacek Kusnierczyk
Duncan Murdoch wrote: > Shruthi Jayaram wrote: >> Hi, >> >> How do I check for two conditions in an if loop? I want to check if a >> value >> lies between 2 other values. >> > "if" isn't normally a loop, but what you want is the vectorized > version, the ifelse() function. >> For example, >> A <

Re: [R] if statement

2009-01-05 Thread Carlos J. Gil Bellosta
Hello, If you do C <- A C[A > X & A < Y] <- 0 you get what it seems you want. Best regards, Carlos J. Gil Bellosta http://www.datanalytics.com On Mon, 2009-01-05 at 03:41 -0800, Shruthi Jayaram wrote: > A <- ts(rnorm(120), freq=12, start=c(1992,8)) > X <- 0.5 > Y <- 0.8 _

Re: [R] if statement

2009-01-05 Thread Duncan Murdoch
Shruthi Jayaram wrote: Hi, How do I check for two conditions in an if loop? I want to check if a value lies between 2 other values. "if" isn't normally a loop, but what you want is the vectorized version, the ifelse() function. For example, A <- ts(rnorm(120), freq=12, start=c(1992,8)) X

[R] if statement

2009-01-05 Thread Shruthi Jayaram
Hi, How do I check for two conditions in an if loop? I want to check if a value lies between 2 other values. For example, A <- ts(rnorm(120), freq=12, start=c(1992,8)) X <- 0.5 Y <- 0.8 I would like to create a new vector C for which C[i] is 0 if A[i] lies in between X and Y. Would be grate

Re: [R] if statement problem

2008-01-01 Thread Gabor Grothendieck
Try: das$danger <- with(das, (age > 65) * (bmi > 30)) On Jan 1, 2008 5:03 PM, Gerard Smits <[EMAIL PROTECTED]> wrote: > Hi All, > > I have a small dataset named das (43 cases) in which I am trying to > create a binary outcome (1/0) based on the following code: > > if (das$age>65 && das$bmi>30) {

Re: [R] if statement problem

2008-01-01 Thread Gerard Smits
Hi Domenico, I was incorrectly assuming it would use a vector of equal length to my data. frame. Thanks for the clarification. Also, thanks for the many alternate programming approaches provided by others. Gerard At 02:25 PM 1/1/2008, Domenico Vistocco wrote: >You should look for your answer

Re: [R] if statement problem

2008-01-01 Thread Tim Calkins
08, Christos Hatzis wrote: > >You need to use '&' instead of '&&': > > > >A shorter version of your code using ifelse: > > > >das$danger <- with(das, ifelse(age>65 & bmi>30, 1, 0)) > > > >HTH > > > >-Chr

Re: [R] if statement problem

2008-01-01 Thread Domenico Vistocco
You should look for your answer using the help for the if statement (?"if"). The cond argument should be a scalar (otherwise only the first element is used). ?"if" . cond: A length-one logical vector that is not 'NA'. Conditions of length greater than one are accepted with a warnin

Re: [R] if statement problem

2008-01-01 Thread Gerard Smits
mp; bmi>30, 1, 0)) > >HTH > >-Christos > > > -Original Message- > > From: [EMAIL PROTECTED] > > [mailto:[EMAIL PROTECTED] On Behalf Of Gerard Smits > > Sent: Tuesday, January 01, 2008 5:04 PM > > To: r-help@r-project.org > > Subject: [R] i

Re: [R] if statement problem

2008-01-01 Thread Christos Hatzis
On Behalf Of Gerard Smits > Sent: Tuesday, January 01, 2008 5:04 PM > To: r-help@r-project.org > Subject: [R] if statement problem > > Hi All, > > I have a small dataset named das (43 cases) in which I am > trying to create a binary outcome (1/0) based on the following code

[R] if statement problem

2008-01-01 Thread Gerard Smits
Hi All, I have a small dataset named das (43 cases) in which I am trying to create a binary outcome (1/0) based on the following code: if (das$age>65 && das$bmi>30) {das$danger<-1} else das$danger<-0 I am setting a flag called 'danger' to 1 of the subject is over 65 and has a BMI > 30. I fin