Generation

generate functionSat, 27 May 2023

INPUT ARGUMENTS RETURN OUTPUT “monday” monday is weekday. “tuesday” tuesday is weekday. “wednesday” wednesday is weekday. “thursday” thursday is weekday. “friday” friday is weekday. A.4 Consider a vector of integers in R, called "numbers", containing the following values: 9, 3, 12, 2, 17. You need to perform the following tasks using loops (for, while, or repeat) in R: a) Print the square of each number in the "numbers" vector. b) Calculate the sum of the cube of all the numbers in the "numbers" vector. A.5 Create a function “weekday_weekend” which suggests whether the day is weekday or weekend. (Hint: Use If…Else… Statements). The function should print output something like this: “saturday” saturday is weekend. “sunday” sunday is weekend. A.6 Create a vector “days” which consists of names of all seven days in a week. Check for the “weekday_weekend” function on each element of the vector days. (Hint: Using for loop).

library(tidyverse) numbers <- c(9, 3, 12, 2, 17) for (num in numbers) { square <- num^2 print(num) } sum <- 0 for (num in numbers) { cube <- num^3 sum <- sum + cube } print(sum) weekday <- c('monday','tuesday','wednesday','thursday','friday') weekend <- c('saturday','sunday') weekday_weekend <- function(day){ if (day %in% weekday) { print(paste(day,' is weekday.', sep = '')) } else if (day %in% weekend) { print(paste(day,' is weekend.', sep = '')) } else { print('Please enter a valid day!') } } days <- c('monday','tuesday','wednesday','thursday','friday','saturday','sunday') for (day in days) {

Questions about programming?Chat with your personal AI assistant