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
Using ?replace()
vec2 <- replace(n,c(n[n%%3==0 & !n%%5==0],n[!n%%3==0 &
n%%5==0],n[n%%15==0]),c(rep(c("heads","tails","headstails"),c(sum(n%%3==0 &
!n%%5==0),sum(!n%%3==0 & n%%5==0),sum(n%%15==0)
identical(vec1,vec2)
#[1] TRUE
#or
library(plyr)
vec3 <- mapvalues(n,c(n[n%%3==0 & !n%%5==0
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
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
multip
4 matches
Mail list logo