Question

In: Computer Science

Use R code to do the following!!!! 1. Create a vector v1 that will contain integers...

Use R code to do the following!!!!

1. Create a vector v1 that will contain integers from -30 to 60.
2. Copy v1 into a vector v2 and add names 'odd' or 'even' based on the value.
3. Copy v1 into a vector v3 and if the number can be divided by 3, replace it by 'NA'.
4. Assign the mean of v3 to v4 ignoring the 'NA'.

Solutions

Expert Solution

Screenshot

Program

#1. Create a vector v1 that will contain integers from -30 to 60.
v1 <- -30:60;
#2. Copy v1 into a vector v2 and add names 'odd' or 'even' based on the value.
v2=vector();
for(i in 1:length(v1)){
     if(v1[i]%%2==0){
          v2[i]='even';
     }
    else{
        v2[i]='odd';
    }
}
#Copy v1 into a vector v3 and if the number can be divided by 3, replace it by 'NA'.
v3=vector();
for(i in 1:length(v1)){
     if(v1[i]%%3==0){
          v3[i]=NA;
     }
    else{
        v3[i]=v1[i];
    }
}
#Assign the mean of v3 to v4 ignoring the 'NA'.
v4=mean(v3,trim=0,na.rm=TRUE);
#Display all
cat("Values of v1 vector:-\n")
print(v1)
cat("\nValues of v2 vector:-\n")
print(v2)
cat("\nValues of v3 vector:-\n")
print(v3)
cat("\nValues of mean in v4:-\n")
print(v4)

------------------------------------------------------

Output

Values of v1 vector:-
[1] -30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15 -14 -13 -12
[20] -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1   0   1   2   3   4   5   6   7
[39]   8   9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
[58] 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
[77] 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

Values of v2 vector:-
[1] "even" "odd" "even" "odd" "even" "odd" "even" "odd" "even" "odd"
[11] "even" "odd" "even" "odd" "even" "odd" "even" "odd" "even" "odd"
[21] "even" "odd" "even" "odd" "even" "odd" "even" "odd" "even" "odd"
[31] "even" "odd" "even" "odd" "even" "odd" "even" "odd" "even" "odd"
[41] "even" "odd" "even" "odd" "even" "odd" "even" "odd" "even" "odd"
[51] "even" "odd" "even" "odd" "even" "odd" "even" "odd" "even" "odd"
[61] "even" "odd" "even" "odd" "even" "odd" "even" "odd" "even" "odd"
[71] "even" "odd" "even" "odd" "even" "odd" "even" "odd" "even" "odd"
[81] "even" "odd" "even" "odd" "even" "odd" "even" "odd" "even" "odd"
[91] "even"

Values of v3 vector:-
[1] NA -29 -28 NA -26 -25 NA -23 -22 NA -20 -19 NA -17 -16 NA -14 -13 NA
[20] -11 -10 NA -8 -7 NA -5 -4 NA -2 -1 NA   1   2 NA   4   5 NA   7
[39]   8 NA 10 11 NA 13 14 NA 16 17 NA 19 20 NA 22 23 NA 25 26
[58] NA 28 29 NA 31 32 NA 34 35 NA 37 38 NA 40 41 NA 43 44 NA
[77] 46 47 NA 49 50 NA 52 53 NA 55 56 NA 58 59 NA

Values of mean in v4:-
[1] 15


Related Solutions

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...
Use R to do each of the following. Use R code instructions that are as general...
Use R to do each of the following. Use R code instructions that are as general as possible, and also as efficient as possible. Use the Quick-R website for help on finding commands. 1. The following is a random sample of CT scores selected from 32 Miami students. 28, 27, 29, 27, 29, 31, 32, 30, 34, 30, 27, 25, 30, 32, 35, 32 23, 26, 27, 33, 33, 33, 31, 25, 28, 34, 30, 33, 28, 26, 30, 28...
Find the 95% confidence interval of the mean of a vector in r code. The vector...
Find the 95% confidence interval of the mean of a vector in r code. The vector length is 100.
Create a vector of 100 integers PRG from 1 to 500. Find the max and min...
Create a vector of 100 integers PRG from 1 to 500. Find the max and min and print those out. Please use c++.
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 //...
R Programming: create a vector for 1 to 31 and count the number of even and...
R Programming: create a vector for 1 to 31 and count the number of even and odds using ifelse()
Code in Java Create a stack class to store integers and implement following methods: 1) void...
Code in Java Create a stack class to store integers and implement following methods: 1) void push(int num): This method will push an integer to the top of the stack. 2) int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3) void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
R Studio Coding Exercise Problem-Set Questions 1-6 # 1) Create the following vector in 1 line...
R Studio Coding Exercise Problem-Set Questions 1-6 # 1) Create the following vector in 1 line of code without using the c() function: # [i] 4 12 20 4 12 20 4 12 # 2) Create a vector of 25 random heights between 54 and 78 inches. Cycle through the vector using a For loop and create a new vector that places each height into a category. People less than 5 feet should be categorized as short, those taller than...
Do not use arrays and code a program that reads a sequence of positive integers from...
Do not use arrays and code a program that reads a sequence of positive integers from the keyboard and finds the largest and the smallest of them along with their number of occurrences. The user enters zero to terminate the input. If the user enters a negative number, the program displays an error and continues. Sample 1: Enter numbers: 3 2 6 2 2 6 6 6 5 6 0 Largest Occurrences Smallest Occurrences 6 5 2 3. Do not...
Create a code for A vector for forecasting and the value of alpha and provides an...
Create a code for A vector for forecasting and the value of alpha and provides an exponential smoothing forecast for the given dataset within R.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT