In: Computer Science
The corresponding age ranges are
(3,13],(13,18],(18,60],and (60,120]. For example, one must be older
than 3 and not older than 13 to be called a child. The contents of
ageFac should be as follows:
[1] child adult child senior adult child adult
[8] adult adult teen
Levels: child teen adult senior
Below is the R code to execute all the above answers:
##code starts here
##character vector StatusFac
statusDesc <- c("freshman","sophomore","junior","senior");
statusFac<- as.factor(statusDesc)
statusFac
##assign 10 random numbers
set.seed(1)
ages<-rnorm(10,24,24);
ageFac <- cut(ages, breaks = c(3,13,18,60,120), labels =
c("child","teen","adult","senior"));
ageFac
##prompt for number and check the number
## num <- as.integer(readline(prompt = "Enter number"));
num <- 5;
if(num%%2==0){
print("it is even number");
} else{
print("it is odd number");
}
##score and grade as numeric variables
score <- 95; ##you can alternatively use readline function to
ask score input from the user
if(score>=90){
grade <-"A";
} else if(score >=80){
grade <- "B";
} else if(score >= 70){
grade <- "C";
}
grade
##a,b, c and, d vectors
a<-c(2,-4,-3,7,6);
b<-1:5;
c<- 6:10;
d <- vector(mode = "numeric", length = 0);
for(i in 1:5){
if(a[i]>0){
d[i]=b[i];
}
else{
d[i]=c[i];
}
}
d
##“Hello World!” 10 times
i<-1;
repeat{
print("Hello World");
if(i==10){
break;
}
i<-i+1;
}
##while loop to calculate factorial 9
i<-1;
factorial<-1;
while(i<=9){
factorial <- factorial *i;
i<-i+1;
}
factorial
##even numbers
data <- seq(0,100,2);
sumdata<-0;
for(i in data){
sumdata = sumdata +i;
}
sumdata
##code ends here
screenshot of code from RStudio