# NOT RUN {
# leave only 'setosa'
where(iris, Species == "setosa")
# leave only first five rows
where(iris, 1:5)
# infix version
# note that '%where%' have higher precendence than '=='
# so we need to put condition inside brackets
iris %where% (Species == "setosa")
iris %where% 1:5
# example of .N usage.
set.seed(42)
train = iris %where% sample(.N, 100)
str(train)
set.seed(42)
test = iris %where% -sample(.N, 100)
str(test)
# list example
set.seed(123)
rand_matr = matrix(sample(10, 60, replace = TRUE), ncol = 3)
rand_vec = sample(10, 20, replace = TRUE)
my_list = list(iris, rand_matr, rand_vec)
# two random elements from the each list item
where(my_list, sample(.N, 2))
# }
Run the code above in your browser using DataLab