[R] Using sample to create Training and Test sets

Chris Arthur chris.arthur at bristol.ac.uk
Fri May 15 09:42:54 CEST 2009

Forgive the newbie question, I want to select random rows from my 
data.frame to create a test set (which I can do) but then I want to 
create a training set using whats left over.

Example code:
acc <- read.table("accOUT.txt", header=T, sep = ",", row.names=1)
#select 400 random rows in data
training <- acc[sample(1:nrow(acc), 400, replace=TRUE),]

#try to get whats left of acc not in training
testset <- acc[-training, ]
Fails with the following error....
Error: invalid subscript type
In addition: Warning message:
- not meaningful for factors in: Ops.factor(left)

I then try.
testset <- acc[!training, ]
Which gives me the warning message
! not meaningful for factors in: Ops.factor(left)
And if i look at testset It is 400 rows of NA's ... which clearly isn't 

Can anyone tell me what I'm doing wrong.

Thanks in advance


More information about the R-help mailing list