Question: Given x < - c ( 1 , 2 , 7 , 3 , 6 , 8 , 1 2 , 1 3 , 1

Given x <- c(1,2,7,3,6,8,12,13,15,18), how do you count the number of even numbers in a vector x using a for loop in R?
Question 11Answer
a.
count <- sum(x %%2==0)
b.
count <-0; while (x >0){ if (x %%2==0){ count = count +1} x = x -1}
c.
count <-0; for (val in x){ if (val %%2!=0){ count = count +1}}
d.
count <-0; for (val in x){ if (val %%2==0){ count = count +1}}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!