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...
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'.
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
In R-Syntax, create a vector of 100 employees ("Employee 1", "Employee 2", ... "Employee 100") [Hint:...
In R-Syntax, create a vector of 100 employees ("Employee 1", "Employee 2", ... "Employee 100") [Hint: use the `paste()` function and vector recycling to add a number to the word "Employee"] then create a vector of 100 random salaries for the year 2017 [Use the `runif()` function to pick random numbers between 40000 and 50000] and finally create a vector of 100 salary adjustments between -5000 and 10000 [use runif() to pick 100 random #'s in that range]
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT