Friday, October 19, 2018

subset - How to write the remaining data frame in R after randomly subseting the data

sample sets a random seed each time you run it, thus if you want to reproduce its results you will either need to set.seed or save its results in a variable.


Addressing your question, you simply need to add - before your index in order to get the rest of the data set.
Also, don't forget to add a comma after the indx if you want to select rows (unlike in your question)


set.seed(1)
indx <- sample(nrow(df), 3)

Your subset


df[indx, ]
# x y z
# 2 1 5 b
# 6 3 1 f
# 3 2 4 c

Remaining data set


df[-indx, ]
# x y z
# 1 1 6 a
# 4 2 3 d
# 5 3 2 e

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...