joran.g
joran.g

Reputation: 89

How do I get the non-selected rownumbers?

I created a training and testing subset from my original data:

df <- data.frame(var = seq(1, 200, by = 2))
train.rows <- sample(1:100, 75, replace = FALSE)
df.train <- df[train.rows,]
df.test <- df[-train.rows,]

How can I see the numbers of the rows from the original dataframe (df) that I selected to be in df.test? (To see the ones that I selected to be in df.train, I would just check out trains.rows. But that is not possible for -train.rows)

Edit: the title is about the fact that I specify/select rows for a training set and now I just want to know the ones that I didn't select for the training set

Upvotes: 0

Views: 33

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389235

There are multiple ways you can approach this :

1) Since this is one-column dataframe use drop = FALSE

df.train <- df[train.rows,, drop = FALSE]
df.test <- df[-train.rows,, drop = FALSE]

You can now see row numbers in df.train and df.test which can be extracted with rownames(df.train) and rownames(df.test).

2) You can use setdiff to include the index of rows which is present in df.test.

setdiff(1:nrow(df), train.rows)

Upvotes: 1

Related Questions