Open mhorger opened 5 years ago
Good working solution!
The issue here has to do with R's vector variable. You are attempting to store numbers and string characters in the FB vector; and R does not allow vectors to store both types at the same time. You can only store all numbers (say numerics or integers), or all strings (characters).
FB <- c(1:100) #FB contains 100 numbers
for (i in 1:100) {
if (i%%3==0)
FB[i]<-"Fizz" #Turns all items in FB into strings because you added one string
}
There is no way around this issue because the vector variable can only store a number or string. So, having a final answer that looks like this, with numbers treated as characters
"1","2","fizz","4" etc.
is totally fine.
So, I can get the solution to this problem fine, but I either end up with quotations or all integers and the words come up as unavailable.
Here's the code I'm using: FB <- c(1:100) for (i in 1:100) { if (i%%3==0) FB[i]<-"Fizz" } for (j in 1:100){ if (j%%5==0)
FB[j]<-"Buzz"
} for (k in 1:100){ if (k%%3 ==0 & k%%5 == 0) FB[k]<-"FizzBuzz" else FB<-as.integer(FB) }I've tried adding that bolded line at the very bottom to specify or change the class. I've also tried adding this code to the bottom: for(m in 1:100){ if (m == 1:100) FB[m]<-as.integer(m) }
Anyone experience a similar problem?