Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Add sample function for DataFrames #997

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/DataFrames.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export @~,
readtable,
rename!,
rename,
sample,
showcols,
stack,
stackdf,
Expand Down Expand Up @@ -111,7 +112,9 @@ for (dir, filename) in [
("statsmodels", "statsmodel.jl"),

("", "RDA.jl"),
("", "deprecated.jl")
("", "deprecated.jl"),

("other", "sample.jl"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add this to "utils.jl", no need for a separate file.

]

include(joinpath(dir, filename))
Expand Down
31 changes: 31 additions & 0 deletions src/other/sample.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import StatsBase: sample

"""
sample(df[, n; replace=true, ordered=false])

Draw a random sample of `n` rows from a data frame `df` and return the result as a data frame.

# Arguments

- `replace::Bool=true`: Should sampling be performed with replacement?
- `ordered::Bool=false`: Should an ordered sample be taken?

# Example
```
julia> using RDatasets
julia> iris = dataset("datasets", "iris")
julia> srand(1)
julia> sample(iris, 5)
5×5 DataFrames.DataFrame
│ Row │ SepalLength │ SepalWidth │ PetalLength │ PetalWidth │ Species │
|-----|-------------|------------|-------------|------------|--------------|
│ 1 │ 5.0 │ 2.0 │ 3.5 │ 1.0 │ "versicolor" │
│ 2 │ 6.2 │ 2.9 │ 4.3 │ 1.3 │ "versicolor" │
│ 3 │ 6.7 │ 3.1 │ 4.7 │ 1.5 │ "versicolor" │
│ 4 │ 5.5 │ 2.3 │ 4.0 │ 1.3 │ "versicolor" │
│ 5 │ 5.8 │ 2.7 │ 5.1 │ 1.9 │ "virginica" │
```
"""
function sample(df::AbstractDataFrame, n::Integer=1; replace::Bool=true, ordered::Bool=false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should extend the function from StatsBase, i.e. function StatsBase.sample(...)

df[sample(1:size(df, 1), n, replace=replace, ordered=ordered), :]
end
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ my_tests = ["utils.jl",
"iteration.jl",
"duplicates.jl",
"show.jl",
"statsmodel.jl"]
"statsmodel.jl",
"sample.jl"]

println("Running tests:")

Expand Down
10 changes: 10 additions & 0 deletions test/sample.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module TestSample
using Base.Test
using DataFrames

df = DataFrame(A=1:10,B=11:20)
srand(1)
df_sample = sample(df, 5)
@test df_sample[:A] == [1,8,7,4,2]
@test df_sample[:B] == [11,18,17,14,12]
end