Store 10 values in R

1

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.

    
asked by anonymous 27.09.2017 / 21:25

1 answer

1

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
    
28.09.2017 / 19:41