Question

In: Computer Science

Considering a Character vector called statusDesc=c(“senior”,”sophomore”,”junior”,”freshman”). Write R code(s) to create a factor called statusFac that...

  1. Considering a Character vector called
    statusDesc=c(“senior”,”sophomore”,”junior”,”freshman”).
    1. Write R code(s) to create a factor called statusFac that will take statusDesc as an argument and has four levels in the order of “freshman,” ”sophomore,” ”junior,” and “senior.”
  2. Use the following codes to generate 10 random numbers to represent 10 random ages. set.seed(1)
    ages=rnorm(10,24,24)
    1. Based on the variable ages, use cut() to create a factor called ageFac that has 4 levels “child”,”teen”,”adult”, and “senior”.  

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

  1. Assuming a variable called num contains an integer,
    1. write an if-else statement to display the message “it is an even number” if num is even, or “it is an odd number” if num is odd.
      (Hint: use %% 2 to check the remainder of num divided by 2).
  2. Consider two variables, score and grade. The variable score contains a numeric value.
    1. Write an if-else if statement to:
      1. assign “A” to grade if score is greater than or equal to 90;
      2. assign “B” to grade if score is greater than or equal to 80 but less than 90; or
      3. assign “C” to grade if score is greater than or equal to 70 but less than 80.
  3. Considering the following R codes
    a=c(2,-4,-3,7,6)
    b=1:5
    c=6:10
    There is a numeric vector d.
    1. Assign the item in b to d if the corresponding item in a is greater than 0,
      otherwise, assign the item in c to d.
      For instance, the first item in a is 2, which is greater than 0, therefore the first
      item of d should be 1, which comes from b. (Hint: use if-else statement).
  4. Write a repeat loop to print out the message “Hello World!” 10 times.
  5. Write a while loop to calculate 9! (factorial).
  6. Considering a vector data=seq(0,100,2), which represents all the even numbers from 0 through 100,
    1. write a “for loop” to sum up all the items in data.

Solutions

Expert Solution

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


Related Solutions

a) Write C code using if statements to display ‘Freshman, ‘Sophomore’, ‘Junior’ or ‘Senior’ based on...
a) Write C code using if statements to display ‘Freshman, ‘Sophomore’, ‘Junior’ or ‘Senior’ based on what value is in variable classification. Display ‘unknown ’ if classification does not contain ‘f’, ‘s’, ‘j’or ‘r’. Allow upper or lower case in variable color. In other words either ‘r’ or ‘R’ will display ‘Senior’. b) Write C code to do the same thing as the previous problem, but use a switch statement.
12. Political Party Affiliation and Class level Freshman Sophomore Junior Senior Total Democrat 1 4 5...
12. Political Party Affiliation and Class level Freshman Sophomore Junior Senior Total Democrat 1 4 5 3 13 Republican 4 8 4 2 18 Green 1 3 3 2 9 Libertarian 1 1 2 1 5 Peace/Freedom 2 3 3 2 10 Total 6 15 12 7 55 Perform a hypothesis test to determine whether there is an association between class levels and political party affiliation.
Use R code Create a vector V with 8 elements (7,2,1,0,3,-1,-3,4): Transform that vector into a...
Use R code Create a vector V with 8 elements (7,2,1,0,3,-1,-3,4): Transform that vector into a rectangular matrix A of dimensions 4X2 (4- rows, 2-columns); Create a matrix transpose to the above matrix A. Call that matrix AT; Calculate matrix products: A*AT and AT*A. Present the results. What are the dimensions of those two product matrices; Square matrixes sometimes have an inverse matrix. Try calculating inverse matrices (or matrixes, if you prefer) of above matrices (matrixes) A*AT and AT*A; Extend...
Write C code to create a structure called time_of_day, which stores the current time in hours,...
Write C code to create a structure called time_of_day, which stores the current time in hours, minutes, and seconds. All the fields should be integers except for seconds, which should be a floating point value. Write a C function called check_time, which takes a pointer to a time_of_day structure as input, and return 1 if the time is valid (0 to 23 hours, 0 to 59 minutes, 0 to 59.999999 seconds) and 0 otherwise. Assume that times are stored in...
How do I write a C# and a C++ code for creating a character array containing...
How do I write a C# and a C++ code for creating a character array containing the characters 'p', 'i', 'n','e','P','I','N','E' only and then using these lower and capital case letter character generate all possible combinations like PInE or PinE or PIne or PINE or piNE etc. and only in this order so if this order is created eg. NeIP or EnPi or NeIP or IPnE and on. You can generate all the combinations randomly by creating the word pine...
Programming in C language (not C++) Write a function definition called PhoneType that takes one character...
Programming in C language (not C++) Write a function definition called PhoneType that takes one character argument/ parameter called "phone" and returns a double. When the variable argument phone contains the caracter a or A, print the word Apple and return 1099.99. When phone contains the caracter s or S print the word Samsung and return 999.99. When phone contains anything else, return 0.0.
In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and...
In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and returns a double. When the variable argument phone contains the character a or A print the word Apple and return 1099.99 When phone contains anything else return a 0.0
Code in C++ Objectives Use STL vector to create a wrapper class. Create Class: Planet Planet...
Code in C++ Objectives Use STL vector to create a wrapper class. Create Class: Planet Planet will be a simple class consisting of three fields: name: string representing the name of a planet, such as “Mars” madeOf: string representing the main element of the planet alienPopulation: int representing if the number of aliens living on the planet Your Planet class should have the following methods: Planet(name, madeOf, alienPopulation) // Constructor getName(): string // Returns a planet’s name getMadeOf(): String //...
Consider a numeric vector x (e.g., x=c(2,3,-1,1.2,2.6,0)), write the R code that determine whether each element...
Consider a numeric vector x (e.g., x=c(2,3,-1,1.2,2.6,0)), write the R code that determine whether each element of x is (Verify that your code work correctly): (a) positive (b) non-positive (c) even or odd (d) an integer
Write a function in c++, called afterAll that takes two parameters, a vector of string and...
Write a function in c++, called afterAll that takes two parameters, a vector of string and a string. The function returns true if the 2nd parameter comes after all of the strings in the vector, order-wise, false if not. As an example, "zoo" comes after "yuzu".
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT