Jamie Jamie - 1 month ago 10
R Question

How to analyse each individual vector value in an if statement

I'm trying to use an if statement to analyse the number of user-inputted values greater than or equal to 70. For every value they input greater than (or equal to) 70, I want the variable

totalFirst
to increase by 1. However, I am not sure how to make an if statement analyse each individual value in a vector.

Code so far:

totalFirst <- 0

prompt <- "What were your second year module results?(space separated)"
secondYearMark <- as.integer(strsplit(readline(prompt), " ")[[1]])
if (secondYearMark >= 70) totalFirst <- totalFirst + 1

dww dww
Answer

The simplest way is to use sum

prompt <- "What were your second year module results?(space separated)"
secondYearMark <- as.integer(strsplit(readline(prompt), " ")[[1]])
totalFirst <- sum(secondYearMark >= 70)