Hey listers, I am trying to do something simple... Check the program below... I would like to create a variable named COLOR according to the conditions that I stablished... But the problem is that it seems that my variable COLOR is checking just on sample, may be last in the loop... Certainly, I am missing something... Thanks in advance, Marcio x<-c(288,139,196,159,536,134,623,517,96,467,277,155,386,241,422,6263,612,532,250,412,339,55,290,249,164,97,74,144,1277,240,163,63,488,111,128,230,720,179,37,24,65,37,89,187,60,939,1008,81,310,58,169,38,68,190,78,807,220,226,69,179129,119,73,59,92,127,104,75,505,183,49,41,76,113,90,79,408,140,200,284,103,58,654,118,431,192,233,102,97,56,69,73,86,53,105,81,77,472,129,194,299,81,122,113,186,91,145,133,114,78,78,72,70,3471,641,275,815,149,185,172,240,67,526,122,229,298,317,179,233,66,129,87,82,63,65,72,6720,381,240,118,396,66,35,43,166,216,53,82,90,62,77,207,68,52,277,396,220,751,146,95,37,35,39,46,59,44,105,87,66,62,175,252,128,330,57,83,208,74,63,109,37,105,38,82,76,63,86,603,209,100,121,191,130,63,128,90,79,50,1025,121,87,309,75,189,36,82,84,60,132,46,965,155,132,219,112,53,90,66,100,77,52,60,100,153,418,392,76,130,197,262,49,105,87,70,147,720,342,233,203,249,92,134,231,782,184,182,432,49,63,94,124,69,53,91,451,53,21,42,50,40,32,58,26,28,61,60,35,764,105,592,55,28,46,34,123,41,54,207,64,562,295,226,63,233) R<-142 color<-rep(0,142) for(i in 1:R){ x<-sample(x,142,replace=FALSE) if (!3471 %in% x & !6263 %in% x & !6720 %in% x){color[i]<-1} else if (3471 %in% x & !6263 %in% x & !6720 %in% x){color[i]<-2} else if (!3471 %in% x & 6263 %in% x & !6720 %in% x){color[i]<-3} else if (!3471 %in% x & !6263 %in% x & 6720 %in% x){color[i]<-4} else if (3471 %in% x & 6263 %in% x & !6720 %in% x){color[i]<-5} else if (3471 %in% x & !6263 %in% x & 6720 %in% x){color[i]<-6} else if (!3471 %in% x & 6263 %in% x & 6720 %in% x){color[i]<-7} else if (3471 %in% x & 6263 %in% x & 6720 %in% x){color[i]<-8} else{color[i]<-0} } -- View this message in context: http://r.789695.n4.nabble.com/If-then-else-with-command-for-tp2539341p2539341.html Sent from the R help mailing list archive at Nabble.com.
Marcio, Try this instead: x<-c(288,139,196,159,536,134,623,517,96,467,277,155,386,241,422,6263,612,532,250,412,339,55,290,249,164,97,74,144,1277,240,163,63,488,111,128,230,720,179,37,24,65,37,89,187,60,939,1008,81,310,58,169,38,68,190,78,807,220,226,69,179129,119,73,59,92,127,104,75,505,183,49,41,76,113,90,79,408,140,200,284,103,58,654,118,431,192,233,102,97,56,69,73,86,53,105,81,77,472,129,194,299,81,122,113,186,91,145,133,114,78,78,72,70,3471,641,275,815,149,185,172,240,67,526,122,229,298,317,179,233,66,129,87,82,63,65,72,6720,381,240,118,396,66,35,43,166,216,53,82,90,62,77,207,68,52,277,396,220,751,146,95,37,35,39,46,59,44,105,87,66,62,175,252,128,330,57,83,208,74,63,109,37,105,38,82,76,63,86,603,209,100,121,191,130,63,128,90,79,50,1025,121,87,309,75,189,36,82,84,60,132,46,965,155,132,219,112,53,90,66,100,77,52,60,100,153,418,392,76,130,197,262,49,105,87,70,147,720,342,233,203,249,92,134,231,782,184,182,432,49,63,94,124,69,53,91,451,53,21,42,50,40,32,58,26,28,61,60,35,764,105,592,55,28,46,34,123,41,54,207,64,562,295,226,63,233) R<-142 color<-rep(0,142) for(i in 1:R){ y<-sample(x,142,replace=FALSE) if (!3471 %in% y & !6263 %in% y & !6720 %in% y){color[i]<-1} else if (3471 %in% y & !6263 %in% y & !6720 %in% y){color[i]<-2} else if (!3471 %in% y & 6263 %in% y & !6720 %in% y){color[i]<-3} else if (!3471 %in% y & !6263 %in% y & 6720 %in% y){color[i]<-4} else if (3471 %in% y & 6263 %in% y & !6720 %in% y){color[i]<-5} else if (3471 %in% y & !6263 %in% y & 6720 %in% y){color[i]<-6} else if (!3471 %in% y & 6263 %in% y & 6720 %in% y){color[i]<-7} else if (3471 %in% y & 6263 %in% y & 6720 %in% y){color[i]<-8} else{color[i]<-0} } Before one of the commands in your loop was: x <- sample(x,142,replace=FALSE) when you did this you altered your assignment of x to be the sample of your original x with a length of 142. Subsequent iterations of the loop had the same data as the original iteration, but sampled randomly. Your if statements were good, you just redefined x in your first iteration of the loop. Adrienne Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina North Carolina State University On Tue, Sep 14, 2010 at 1:29 PM, Mestat <mestat@pop.com.br> wrote:> > Hey listers, > I am trying to do something simple... Check the program below... > I would like to create a variable named COLOR according to the conditions > that I stablished... But the problem is that it seems that my variable > COLOR > is checking just on sample, may be last in the loop... Certainly, I am > missing something... > Thanks in advance, > Marcio > > > x<-c(288,139,196,159,536,134,623,517,96,467,277,155,386,241,422,6263,612,532,250,412,339,55,290,249,164,97,74,144,1277,240,163,63,488,111,128,230,720,179,37,24,65,37,89,187,60,939,1008,81,310,58,169,38,68,190,78,807,220,226,69,179129,119,73,59,92,127,104,75,505,183,49,41,76,113,90,79,408,140,200,284,103,58,654,118,431,192,233,102,97,56,69,73,86,53,105,81,77,472,129,194,299,81,122,113,186,91,145,133,114,78,78,72,70,3471,641,275,815,149,185,172,240,67,526,122,229,298,317,179,233,66,129,87,82,63,65,72,6720,381,240,118,396,66,35,43,166,216,53,82,90,62,77,207,68,52,277,396,220,751,146,95,37,35,39,46,59,44,105,87,66,62,175,252,128,330,57,83,208,74,63,109,37,105,38,82,76,63,86,603,209,100,121,191,130,63,128,90,79,50,1025,121,87,309,75,189,36,82,84,60,132,46,965,155,132,219,112,53,90,66,100,77,52,60,100,153,418,392,76,130,197,262,49,105,87,70,147,720,342,233,203,249,92,134,231,782,184,182,432,49,63,94,124,69,53,91,451,53,21,42,50,40,32,58,26,28,61,60,35,764,105,592,55,28,46,34,123,4! > 1,54,207,64,562,295,226,63,233) > R<-142 > color<-rep(0,142) > for(i in 1:R){ > x<-sample(x,142,replace=FALSE) > if (!3471 %in% x & !6263 %in% x & !6720 %in% x){color[i]<-1} else > if (3471 %in% x & !6263 %in% x & !6720 %in% x){color[i]<-2} else > if (!3471 %in% x & 6263 %in% x & !6720 %in% x){color[i]<-3} else > if (!3471 %in% x & !6263 %in% x & 6720 %in% x){color[i]<-4} else > if (3471 %in% x & 6263 %in% x & !6720 %in% x){color[i]<-5} else > if (3471 %in% x & !6263 %in% x & 6720 %in% x){color[i]<-6} else > if (!3471 %in% x & 6263 %in% x & 6720 %in% x){color[i]<-7} else > if (3471 %in% x & 6263 %in% x & 6720 %in% x){color[i]<-8} else{color[i]<-0} > } > > -- > View this message in context: > http://r.789695.n4.nabble.com/If-then-else-with-command-for-tp2539341p2539341.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Yep... Got that... I just changed my data name... It worked fine... Thanks... -- View this message in context: http://r.789695.n4.nabble.com/If-then-else-with-command-for-tp2539341p2539429.html Sent from the R help mailing list archive at Nabble.com.
Try this: x <- c(288,139,196,159,536,134,623,517,96,467,277,155,386,241,422,6263,612,532,250,412,339,55,290,249,164,97,74,144,1277,240,163,63,488,111,128,230,720,179,37,24,65,37,89,187,60,939,1008,81,310,58,169,38,68,190,78,807,220,226,69,179129,119,73,59,92,127,104,75,505,183,49,41,76,113,90,79,408,140,200,284,103,58,654,118,431,192,233,102,97,56,69,73,86,53,105,81,77,472,129,194,299,81,122,113,186,91,145,133,114,78,78,72,70,3471,641,275,815,149,185,172,240,67,526,122,229,298,317,179,233,66,129,87,82,63,65,72,6720,381,240,118,396,66,35,43,166,216,53,82,90,62,77,207,68,52,277,396,220,751,146,95,37,35,39,46,59,44,105,87,66,62,175,252,128,330,57,83,208,74,63,109,37,105,38,82,76,63,86,603,209,100,121,191,130,63,128,90,79,50,1025,121,87,309,75,189,36,82,84,60,132,46,965,155,132,219,112,53,90,66,100,77,52,60,100,153,418,392,76,130,197,262,49,105,87,70,147,720,342,233,203,249,92,134,231,782,184,182,432,49,63,94,124,69,53,91,451,53,21,42,50,40,32,58,26,28,61,60,35,764,105,592,55,28,46,34,123,41,54,207,64,562,295,226,63,233); color <- replicate(142, { y <- sample(x, 142, replace = FALSE); which(apply(mapply(is.element, expand.grid(rep(list(!c(T, F)), 3)), c(3471, 6263, 6720) %in% y ), 1, all)) }) On Tue, Sep 14, 2010 at 2:29 PM, Mestat <mestat@pop.com.br> wrote:> > Hey listers, > I am trying to do something simple... Check the program below... > I would like to create a variable named COLOR according to the conditions > that I stablished... But the problem is that it seems that my variable > COLOR > is checking just on sample, may be last in the loop... Certainly, I am > missing something... > Thanks in advance, > Marcio > > > x<-c(288,139,196,159,536,134,623,517,96,467,277,155,386,241,422,6263,612,532,250,412,339,55,290,249,164,97,74,144,1277,240,163,63,488,111,128,230,720,179,37,24,65,37,89,187,60,939,1008,81,310,58,169,38,68,190,78,807,220,226,69,179129,119,73,59,92,127,104,75,505,183,49,41,76,113,90,79,408,140,200,284,103,58,654,118,431,192,233,102,97,56,69,73,86,53,105,81,77,472,129,194,299,81,122,113,186,91,145,133,114,78,78,72,70,3471,641,275,815,149,185,172,240,67,526,122,229,298,317,179,233,66,129,87,82,63,65,72,6720,381,240,118,396,66,35,43,166,216,53,82,90,62,77,207,68,52,277,396,220,751,146,95,37,35,39,46,59,44,105,87,66,62,175,252,128,330,57,83,208,74,63,109,37,105,38,82,76,63,86,603,209,100,121,191,130,63,128,90,79,50,1025,121,87,309,75,189,36,82,84,60,132,46,965,155,132,219,112,53,90,66,100,77,52,60,100,153,418,392,76,130,197,262,49,105,87,70,147,720,342,233,203,249,92,134,231,782,184,182,432,49,63,94,124,69,53,91,451,53,21,42,50,40,32,58,26,28,61,60,35,764,105,592,55,28,46,34,123,4! > 1,54,207,64,562,295,226,63,233) > R<-142 > color<-rep(0,142) > for(i in 1:R){ > x<-sample(x,142,replace=FALSE) > if (!3471 %in% x & !6263 %in% x & !6720 %in% x){color[i]<-1} else > if (3471 %in% x & !6263 %in% x & !6720 %in% x){color[i]<-2} else > if (!3471 %in% x & 6263 %in% x & !6720 %in% x){color[i]<-3} else > if (!3471 %in% x & !6263 %in% x & 6720 %in% x){color[i]<-4} else > if (3471 %in% x & 6263 %in% x & !6720 %in% x){color[i]<-5} else > if (3471 %in% x & !6263 %in% x & 6720 %in% x){color[i]<-6} else > if (!3471 %in% x & 6263 %in% x & 6720 %in% x){color[i]<-7} else > if (3471 %in% x & 6263 %in% x & 6720 %in% x){color[i]<-8} else{color[i]<-0} > } > > -- > View this message in context: > http://r.789695.n4.nabble.com/If-then-else-with-command-for-tp2539341p2539341.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
not sure how you wanted sampling of x R<-142 color<-rep(0,142) for(i in 1:R){ y<-sample(x,142,replace=FALSE) onebit<- as.numeric(3471 %in% y) twobit<-as.numeric(6720 %in% y)*2 fourbit<-as.numeric(6263 %in% y)*4 colorbit<-onebit+twobit+fourbit+1 color[i]<-colorbit } On Tue, Sep 14, 2010 at 10:29 AM, Mestat <mestat@pop.com.br> wrote:> > Hey listers, > I am trying to do something simple... Check the program below... > I would like to create a variable named COLOR according to the conditions > that I stablished... But the problem is that it seems that my variable > COLOR > is checking just on sample, may be last in the loop... Certainly, I am > missing something... > Thanks in advance, > Marcio > > > x<-c(288,139,196,159,536,134,623,517,96,467,277,155,386,241,422,6263,612,532,250,412,339,55,290,249,164,97,74,144,1277,240,163,63,488,111,128,230,720,179,37,24,65,37,89,187,60,939,1008,81,310,58,169,38,68,190,78,807,220,226,69,179129,119,73,59,92,127,104,75,505,183,49,41,76,113,90,79,408,140,200,284,103,58,654,118,431,192,233,102,97,56,69,73,86,53,105,81,77,472,129,194,299,81,122,113,186,91,145,133,114,78,78,72,70,3471,641,275,815,149,185,172,240,67,526,122,229,298,317,179,233,66,129,87,82,63,65,72,6720,381,240,118,396,66,35,43,166,216,53,82,90,62,77,207,68,52,277,396,220,751,146,95,37,35,39,46,59,44,105,87,66,62,175,252,128,330,57,83,208,74,63,109,37,105,38,82,76,63,86,603,209,100,121,191,130,63,128,90,79,50,1025,121,87,309,75,189,36,82,84,60,132,46,965,155,132,219,112,53,90,66,100,77,52,60,100,153,418,392,76,130,197,262,49,105,87,70,147,720,342,233,203,249,92,134,231,782,184,182,432,49,63,94,124,69,53,91,451,53,21,42,50,40,32,58,26,28,61,60,35,764,105,592,55,28,46,34,123,4! > 1,54,207,64,562,295,226,63,233) > R<-142 > color<-rep(0,142) > for(i in 1:R){ > x<-sample(x,142,replace=FALSE) > if (!3471 %in% x & !6263 %in% x & !6720 %in% x){color[i]<-1} else > if (3471 %in% x & !6263 %in% x & !6720 %in% x){color[i]<-2} else > if (!3471 %in% x & 6263 %in% x & !6720 %in% x){color[i]<-3} else > if (!3471 %in% x & !6263 %in% x & 6720 %in% x){color[i]<-4} else > if (3471 %in% x & 6263 %in% x & !6720 %in% x){color[i]<-5} else > if (3471 %in% x & !6263 %in% x & 6720 %in% x){color[i]<-6} else > if (!3471 %in% x & 6263 %in% x & 6720 %in% x){color[i]<-7} else > if (3471 %in% x & 6263 %in% x & 6720 %in% x){color[i]<-8} else{color[i]<-0} > } > > -- > View this message in context: > http://r.789695.n4.nabble.com/If-then-else-with-command-for-tp2539341p2539341.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]