Read a typed number (R)

Asked

Viewed 65 times

1

Good morning everyone, I need to do a barbadinha script in R where I have to read a typed number and give his predecessor (like, if I write 10 the predecessor is 9).

But I have a problem that I don’t know how to get started...

print("Digite um numero")
//O QUE VAI AQUI PARA LER O NUMERO DIGITADO
nAntecessor= numero-1
print("O antecessor é:", nAntecessor)

I’m using the R Project program...

  • 2

    Maybe numero <- scan().

  • and what goes inside the parentheses of the scan?

  • 1

    Nothing! Try and see. To finish entering numerical data, type <Enter> without entering a new number.

  • @Noisy, put your solution in the answers, so it helps to see that the question has already been solved.

  • Okay, I think it’s a little too much, but it might actually be better.

1 answer

1

The answer is very simple, just use scan without any argument.

print("Digite um numero")
numero <- scan()
nAntecessor <- numero - 1
cat("O antecessor é: ", nAntecessor, "\n")

Note that I changed the print for cat. How was nay resulted once print only uses one argument. To use it would have to be

print(paste("O antecessor é: ", nAntecessor))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.