Hi, I am very new to 'R' ("discovered" it about 2 months ago) and have been trying to teach myself the language using online guides, however I am not a programmer or statistician and so progress is slow. As an exercise, I have been trying to generate the numbers 1 to 100 and replace multiples of 3 with the text "heads", multiples of 5 with the text "tails" and multiples of both 3 and 5 with the text "headstails". So far I have managed to write the functions individually with:> n=c(1:100)> a=replace(n,n%%3==0,"heads") > b=replace(n,n%%5==0,"tails") > c=replace(n,n%%15==0,"headstails")I would like to combine these functions into a single process to produce an output along the lines of: 1,2,"heads",4,"tails","heads",7,8,"heads","tails"... etc I tried the following, without success:> replace(n,c(n%%3==0,n%%5==0,n%%15==0),c("heads","tails,"headstails"))As an 'R' novice I don't really have an idea of how I should approach this problem and unfortunately I have had problems trying to apply the answers given to other replace() and c() questions to my example. I would be grateful if someone could point me in the right direction or provide a similar example of combining simple functions without just giving the answer away - i'd like to learn how to use 'R', rather than just be told the answer. Regards, Kile [[alternative HTML version deleted]]
Hi, Try: ?vec1 <- as.character(factor(1*(n%%3==0)+2*(n%%5==0)+3*(n%%15==0),labels=c("Other","heads","tails","headstails"))) ?vec1[vec1=="Other"] <- which(vec1=="Other") vec1[1:6] #[1] "1"???? "2"???? "heads" "4"???? "tails" "heads" A.K. On Monday, October 14, 2013 12:57 PM, Kile Green <Kile.Green at newcastle.ac.uk> wrote: Hi, I am very new to 'R' ("discovered" it about 2 months ago) and have been trying to teach myself the language using online guides, however I am not a programmer or statistician and so progress is slow. As an exercise, I have been trying to generate the numbers 1 to 100 and replace multiples of 3 with the text "heads", multiples of 5 with the text "tails" and multiples of both 3 and 5 with the text "headstails". So far I have managed to write the functions individually with:> n=c(1:100)> a=replace(n,n%%3==0,"heads") > b=replace(n,n%%5==0,"tails") > c=replace(n,n%%15==0,"headstails")I would like to combine these functions into a single process to produce an output along the lines of: 1,2,"heads",4,"tails","heads",7,8,"heads","tails"... etc I tried the following, without success:> replace(n,c(n%%3==0,n%%5==0,n%%15==0),c("heads","tails,"headstails"))As an 'R' novice I don't really have an idea of how I should approach this problem and unfortunately I have had problems trying to apply the answers given to other replace() and c() questions to my example. I would be grateful if someone could point me in the right direction or provide a similar example of combining simple functions without just giving the answer away - i'd like to learn how to use 'R', rather than just be told the answer. Regards, Kile ??? [[alternative HTML version deleted]] ______________________________________________ R-help at 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.
This is the world-famous "fizzbuzz" problem. You should be able to find lots of implementations by Googling that word. Here's a pointless collection I wrote once: # a really dumb fizzbuzz alg competition #fbfun1 is 2.5x faster than fbfun2 # fbfun3 is 10x faster than fbfun1 # fbfun1 is 2x faster than fbfun4 # fbfun5 is 20x faster than fbrun3 # Those are user times; in most cases the system time is very small indeed. fbfun1 <- function(xfoo) { xfoo<-1:xfoo fbfoo <- 1+(!as.logical(mod(xfoo,3)))*(as.logical(mod(xfoo,5))) + 2*(as.logical(mod(xfoo,3)))*(!as.logical(mod(xfoo,5)))+3*(!as.logical(mod(xfoo,3)))*(!as.logical(mod(xfoo,5))) fbbar <- unlist(lapply(fbfoo, function(x) switch(x,0,'fizz','buzz','fizzbuzz'))) return(fbbar) } fbfun3 <- function(xfoo) { xfoo<-1:xfoo fbfoo <- 1+(!as.logical(mod(xfoo,3)))*(as.logical(mod(xfoo,5))) + 2*(as.logical(mod(xfoo,3)))*(!as.logical(mod(xfoo,5)))+3*(!as.logical(mod(xfoo,3)))*(!as.logical(mod(xfoo,5))) fbtab<-cbind(1:4,c('','fizz','buzz','fizzbuzz')) fbbar <- fbtab[fbfoo,2] return(fbbar) } # can I do it with recycled vectors, e.g. c('','','fizz') and c('','','','','buzz') ? fbfun4 <- function(xfoo) { fiz<- rep(c('','','fizz'),length.out=xfoo) buz<-rep(c('','','','','buzz'),length.out=xfoo) fbbar <- unlist(lapply(1:xfoo, function(j)paste(fiz[j],buz[j]) ) ) return(fbbar) } # or completely sleazy: fbfun5 <- function(xfoo) { fiz<- rep(c('','','fizz','','buzz','fizz','','','fizz','buzz','','fizz','','','fizzbuzz'),length.out=xfoo) return(fiz) } -- View this message in context: http://r.789695.n4.nabble.com/Help-with-combining-functions-tp4678212p4678272.html Sent from the R help mailing list archive at Nabble.com.