I can sample 10 rows from a data.frame like this:
mtcars[sample(1:32, 10),]
What is syntax for doing this with dplyr? This is what I tried:
library(dplyr)
filter(mtcars, sample(1:32, 10))
I can sample 10 rows from a data.frame like this:
mtcars[sample(1:32, 10),]
What is syntax for doing this with dplyr? This is what I tried:
library(dplyr)
filter(mtcars, sample(1:32, 10))
I believe you aren't really "filtering" in your example, you are just sampling rows.
In hadley´s words here is the purpose of the function:
filter() works similarly to subset() except that you can give it any number of filtering conditions which are joined together with & (not && which is easy to do accidentally!)
Here is an example with the mtcars dataset, as it's used in the introductory vignette
library(dplyr)
filter(mtcars, cyl == 8, wt < 3.5)
mpg cyl disp hp drat wt qsec vs am gear carb
1 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
2 15.2 8 304 150 3.15 3.435 17.30 0 0 3 2
3 15.8 8 351 264 4.22 3.170 14.50 0 1 5 4
As a conclusion: filter is equivalen to subset()
, not sample()
.
Figured out how to do it (although Josh O'Brien beat me to it):
filter(mtcars, rownames(mtcars) %in% sample(rownames(mtcars), 10, replace = F))