Reputation: 2233
I would like to let the user enter the dataframe propotion that will be used for training.I tried to use the readline
function but it doesn't work in the code that I've written.How can I pass this value to the "train" object ?
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))),]
EDIT :
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)
The output and error that I get:
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
Upvotes: 1
Views: 638
Reputation: 3678
You could try this :
readinteger <- function(){
as.integer(readline(prompt="Enter an integer(1-9): "))
}
train<-dat[1:round(nrow(dat)*(readinteger()/10)),]
Here is what that gives :
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
or (if you want numbers between 0 and 1 as input) :
readnumeric <- function(){
as.numeric(readline(prompt="Enter a percentage(0.1-0.9): "))
}
train<-dat[1:round(nrow(dat)*readnumeric()),]
Upvotes: 1