强制引入的NA-as.integer

时间:2019-05-18 20:01:21

标签: r rstudio

我写了以下代码

乘法表

num = as.integer(readline(prompt = "Enter the number:"))
   for(i in 1:10)
{
    print(paste(num, 'x', i, '=', num*i))  
}

我得到了以下答案

> #Multiplication table
> num = as.integer(readline(prompt = "Enter the number:"))
Enter the number:for(i in 1:10)
Warning message:
NAs introduced by coercion 
> {
+   print(paste(num, 'x', i, '=', num*i))  
+ }
[1] "NA x 10 = NA"
> 
```

Kindly help

1 个答案:

答案 0 :(得分:0)

问题是由于未输入数字而执行了for循环

num = as.integer(readline(prompt = "Enter the number:"))
Enter the number:10  # entered 10

在下一行中,运行for循环

for(i in 1:10) {
     print(paste(num, 'x', i, '=', num*i))  
 }
#[1] "10 x 1 = 10"
#[1] "10 x 2 = 20"
#[1] "10 x 3 = 30"
#[1] "10 x 4 = 40"
#[1] "10 x 5 = 50"
#[1] "10 x 6 = 60"
#[1] "10 x 7 = 70"
#[1] "10 x 8 = 80"
#[1] "10 x 9 = 90"
#[1] "10 x 10 = 100"