Question

In: Computer Science

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()

Solutions

Expert Solution

  
my_vector<-seq(1,31,1)
print(my_vector)

count_even<-0
count_odd<-0

for(i in my_vector)
{if(i%%2==0)
{count_even<-count_even+1;
}
else
{
count_odd<-count_odd+1;
}
}
cat('no of odd:',count_odd)
cat('\nno of even:',count_even)


Related Solutions

R programming language A) Create a function that accepts two arguments, an integer and a vector...
R programming language A) Create a function that accepts two arguments, an integer and a vector of integers. It returns the count of the number of occurrences of the integer in the input vector. 1]Input: num_count <-function ??? 2]Input: num_count(2,c(1,1,2,2,3,3)) 2] Output: 2 3] Input: num_count(1,c(1,1,2,2,3,1,4,5,5,2,2,1,3)) 3] Output : 4 B) Create a function that accepts 3 integer values and returns their sum. However, if an integer value is evenly divisible by 3, then it does not count towards the...
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...
2.c++ if and loop statement Write a program that will count the number of even number...
2.c++ if and loop statement Write a program that will count the number of even number and odd numbers between two inputted numbers. Display the numbers and compute the sum and average of all the even numbers and the sum and average all the odd numbers. Sample outputs: Enter starting number:3 Enter starting number:4 Enter ending number:10 Enter ending number:10 odd numbers Even number 3 4 5 6 7 8 9 10 number of even numbers=4 number of even numbers=4...
Posting together because they are related. Python 1. Create a program to count the number of...
Posting together because they are related. Python 1. Create a program to count the number of occurrences of each letter of the alphabet in a text file, and print out the total for each letter (if greater than 0). Ignore numbers, punctuation, and special characters. Make sure to treat capital and lowercase letters as occurrences of the same letter. The text file (sample_text.txt) is attached to the assignment. Example: For the text "My dog's name is Winston." The results would...
can a vector with dimensions R^N and a vector with dimensions R^N+1 be a matrix? and...
can a vector with dimensions R^N and a vector with dimensions R^N+1 be a matrix? and if so what would it dimension size be?
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...
In R: write a function that inputs a vector x and a number n and returns...
In R: write a function that inputs a vector x and a number n and returns the first n elements of x. When n is greater than length(x), your function should just return x. We are not allowed to use any control flow statements
This is R language Create a vector that contains the message part of each log entry...
This is R language Create a vector that contains the message part of each log entry in logsin uppercase letters. In your solution, you might want to use the function toupper(). Simply print this vector to the console. # Return vector with uppercase version of message elements in log entries # Apply extract_caps function on logs
Assume the vector AM contains an even number of elements. Develop a MATLAB script that asks...
Assume the vector AM contains an even number of elements. Develop a MATLAB script that asks the user for the vector AM and once the vector is found to contain an even number of elements, divide the product of its even elements by the sum of its odd elements without using the MATLAB functions prod or sum. Store the result in a variable called PDS. If AM is found to contain an odd number of elements, display an error message...
Data Structures for R studio Create a numeric vector containing 10 random numbers ranging from 1...
Data Structures for R studio Create a numeric vector containing 10 random numbers ranging from 1 to 10000. Validate that the object you created is a vector Give an example of where you might find this in a biologicall data set. Create a 10-member list containg both numeric and character data. Validate that the object you created is a list Give an example of where you might find this in a biologicall data set. Create a data frame that relates...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT