Hi to all, I am just looking for more efficient ways ;-) is there a better way instead a loop to decrease x if greater y test <- c(1,3,5,7,9) decrease if greater 1 to test2 <- c(1,2,4,6,8) Kind regards Knut
On Tuesday 18 May 2010, Knut Krueger wrote:> Hi to all, > I am just looking for more efficient ways ;-) > > is there a better way instead a loop to decrease x if greater y > test <- c(1,3,5,7,9) > > decrease if greater 1 to > test2 <- c(1,2,4,6,8)Does this help?> test <- c(1, 3, 5, 7, 9) > test[test > 1] <- test[test > 1] - 1 > test[1] 1 2 4 6 8 Adrian -- Adrian Dusa Romanian Social Data Archive 1, Schitu Magureanu Bd. 050025 Bucharest sector 5 Romania Tel.:+40 21 3126618 \ +40 21 3120210 / int.101 Fax: +40 21 3158391
On 18/05/2010 7:34 AM, Knut Krueger wrote:> Hi to all, > I am just looking for more efficient ways ;-) > > is there a better way instead a loop to decrease x if greater y > test <- c(1,3,5,7,9) > > decrease if greater 1 to > test2 <- c(1,2,4,6,8)test2 <- ifelse( test > 1, test-1, test) Duncan Murdoch
Henrique Dallazuanna
2010-May-18 13:50 UTC
[R] looking for .. dec if vector if element > x
Try this also: pmax(test - 1, 1) On Tue, May 18, 2010 at 8:34 AM, Knut Krueger <rh@krueger-family.de> wrote:> > Hi to all, > I am just looking for more efficient ways ;-) > > is there a better way instead a loop to decrease x if greater y > test <- c(1,3,5,7,9) > > decrease if greater 1 to > test2 <- c(1,2,4,6,8) > > > Kind regards > Knut > > ______________________________________________ > 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]]
Henrique Dallazuanna schrieb:> Try this also: > > pmax(test - 1, 1) > > Otest <- c(1,3,5,7,9,11,12,13,14) test test <- pmax(test - 1, 1) test This works for 1 what about if I would dec 11: to 14 to close the gap between 9 and 10 ? I did not find the answer with the help file Thank you Knut