Good morning everyone, I have to do an exercise where I have to store 10 values typed repeatedly and say how many are negative and positive. I need to use the while statement but I do not know how to store these values.
Good morning everyone, I have to do an exercise where I have to store 10 values typed repeatedly and say how many are negative and positive. I need to use the while statement but I do not know how to store these values.
Try the following:
conta <- 0
x <- numeric(0)
while(conta < 10){
y <- scan(nmax = 1)
x <- c(x, y)
conta <- conta + 1
}
positivo <- x[x > 0]
negativo <- x[x < 0]
zero <- x[x == 0]
Now if you want to know how many positives, negatives or zero (what is missing in the question?) just use length
.
length(positivo)
length(negativo)
length(zero)
To see them,
positivo
negativo
zero