Question

In: Computer Science

How to shorten this "if-else" statements in R studio? Thank you sooo much! fizzbuzz = function(v...

How to shorten this "if-else" statements in R studio? Thank you sooo much!

fizzbuzz = function(v = NULL){
for (term in v){
if (is.infinite(term) != TRUE){
if (is.numeric(term)){
if (term == as.integer(term)){
if (term %% 3 == 0){
if (term %% 5 == 0){
print("FizzBuzz")}
else {
print("Fizz")
}
}
else if (term %% 5 == 0){
print("Buzz")
}
else {
print(term)
}
}
else {
print ("This number is not an integer.")
}
}
else {
print ("This is not a number, or this number was entered as a string.")
}
}
else {
print ("You must enter an integer for this function to work.")
}
}
}

Solutions

Expert Solution

fizzbuzz = function(v = NULL){
   for (term in v){
       if ((is.infinite(term) != TRUE) && is.numeric(term) && (term == as.integer(term))){
           if (term %% 3 == 0 && term %% 5 == 0){
               print("FizzBuzz")
           }
           else if (term %% 5 == 0){
               print("Buzz")
           }
           else if(term %% 3 == 0){
               print("Fizz")
           }
           else {
               print(term)
           }

       }
       else {
           print ("You must enter an integer for this function to work.")
           print ("This is not a number, or this number was entered as a string.")
       }
   }
}

Please let me know in case of any issue


Related Solutions

Please use R studio, Thank you. 2. The probability of a student passing statistics is known...
Please use R studio, Thank you. 2. The probability of a student passing statistics is known to be 0.41; and the probability of a student passing chemistry is known to be 0.55. If the probability of passing both is known to be 0.35, calculate: (a) the probability of passing at least one of statistics and chemistry (b) the probability of a student passing chemistry, given that they passed statistics (c) Are passing chemistry and statistics independent? Justify (d) (harder) a...
Solve following using Program R studio. Please show code and results. Thank you. 1. Assume that...
Solve following using Program R studio. Please show code and results. Thank you. 1. Assume that ? is a random variable follows binomial probability distribution with parameters 15 and 0.25.   a. Simulate 100 binomial pseudorandom numbers from the given distribution (using set.seed(200)) and assign them to vector called binran. b. Calculate ?(? < 8) using cumulative probability function. c. Calculate ?(? = 8) using probability distribution function. d. Calculate the average of simulated data and compare it with the corresponding...
Solve following using Program R studio. Please show code and results. Thank you. 3. Assume that...
Solve following using Program R studio. Please show code and results. Thank you. 3. Assume that ? is a random variable represents lifetime of a certain type of battery which is exponentially distributed with mean 60 hours.   a. Simulate 500 pseudorandom numbers (using set.seed(10)) and assign them to a vector called expran. b. Calculate average of simulated data and compare it with corresponding theoretical value. c. Calculate probability that lifetime is less than 50 hours using cumulative probability function. d....
Hello, i have this excersice in R studio Write a function in R that generates simulations...
Hello, i have this excersice in R studio Write a function in R that generates simulations of a Poisson random variable as follows: define I = [λ], and use p_i + 1 = λp_i / (i + 1) to determine F recursively. Generate a random number U, determine if X≤I by comparing if U≤F (I). If X≤I searches downwards starting at I, otherwise it searches upwards starting from I + 1. Compare the time it takes for the two algorithms...
How do you plot continuous versus category in r studio
How do you plot continuous versus category in r studio
In R studio Write a function that takes as an input a positive integer and uses...
In R studio Write a function that takes as an input a positive integer and uses the print() function to print out all the numbers less than the input integer. (Example: for input 5, the function should print the numbers 1,2,3,4 { for input 1, the function should not print a number.) Use the lapply function, do not use any of the loop commands in your code.
Hey anyone good with R studio? How do you create a square shape with R code...
Hey anyone good with R studio? How do you create a square shape with R code by using plot() and lines() please show your code !
how do you test for regression in R studio with variables x and y
how do you test for regression in R studio with variables x and y
Perform the following tasks on R Studio/R Construct a function called conv3 which inputs a measurement...
Perform the following tasks on R Studio/R Construct a function called conv3 which inputs a measurement in centimeters and outputs the corresponding measurement in inches. However, if a negative value is entered as an input, no conversion of unit is done and an error message is printed instead.
( In R / R studio ) im not sure how to share my data set,...
( In R / R studio ) im not sure how to share my data set, but below is the title of my data set and the 12 columns of my data set. Please answer as best you can wheather its pseudo code, partial answers, or just a suggestion on how i can in to answer the question. thanks #---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- The dataset incovid_sd_20201001.RDatacontains several variables related to infections of covid-19 for eachzip code in San Diego County as of October...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT