为什么readline函数不提示一行并存储给定的输入

时间:2015-11-20 15:51:34

标签: r readline

我想让用户输入将用于培训的数据框规范。我尝试使用readline函数,但它在我编写的代码中不起作用。如何将这个值传递给"火车"对象?

dat <- read.table(text = " IndexRow TargetVar  chairs    tables     lamps  vases
                  1     0        0        0         7  9
                  2     0        0        1         1  6 
                  3     0        1        0         3  5 
                  4     0        1        1         7  8 
                  5     1        0        0         5  4
                  6     1        0        1         1  3 
                  7     1        1        0         0  7
                  8     1        1        1         6  6
                  9     0        0        0         8  9 
                  10    0        0        1         5  3
                  11    1        1        1         4  7 
                  12    0        0        1         2  8
                  13    1        0        0         9  2
                  ", header = TRUE) 

#function to Insert the data splits propotions:
readinteger <- function()
{ 
    tr_split <- readline(prompt="Enter an integer(0.1-0.99): ")
    return(as.integer(tr_split))
}
train<-dat[(1:round(nrow(dat)*(tr_split))),] 

编辑:

dat <- read.table(text = " IndexRow TargetVar  chairs    tables     lamps  vases
                  1     0        0        0         7  9
                  2     0        0        1         1  6 
                  3     0        1        0         3  5 
                  4     0        1        1         7  8 
                  5     1        0        0         5  4
                  6     1        0        1         1  3 
                  7     1        1        0         0  7
                  8     1        1        1         6  6
                  9     0        0        0         8  9 
                  10    0        0        1         5  3
                  11    1        1        1         4  7 
                  12    0        0        1         2  8
                  13    1        0        0         9  2
                  ", header = TRUE) 
train<-dat[1:round(nrow(dat)*(as.integer(readline(prompt="Enter an integer(1-9): "))/10)),]
dim(train)

我得到的输出和错误:

Enter an integer(1-9): dim(train)
Error in 1:round(nrow(dat) * (as.integer(readline(prompt = "Enter an integer(1-9): "))/10)) : 
  NA/NaN argument
In addition: Warning message:
In `[.data.frame`(dat, 1:round(nrow(dat) * (as.integer(readline(prompt = "Enter an integer(1-9): "))/10)),  :
  NAs introduced by coercion

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

readinteger <- function(){ 
    as.integer(readline(prompt="Enter an integer(1-9): "))
}

train<-dat[1:round(nrow(dat)*(readinteger()/10)),]

这是给出的:

train<-dat[1:round(nrow(dat)*(readinteger()/10)),]
Enter an integer(1-9): 5
> train
  IndexRow TargetVar chairs tables lamps vases
1        1         0      0      0     7     9
2        2         0      0      1     1     6
3        3         0      1      0     3     5
4        4         0      1      1     7     8
5        5         1      0      0     5     4
6        6         1      0      1     1     3

或(如果您想要0到1之间的数字作为输入):

readnumeric <- function(){ 
    as.numeric(readline(prompt="Enter a percentage(0.1-0.9): "))
}

train<-dat[1:round(nrow(dat)*readnumeric()),]