diff --git a/NEWS.md b/NEWS.md index ffe6a84..a3f2165 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # Tidier.jl updates +## v0.7.4 - 2023-04-11 +- Added `as_float()`, `as_integer()`, and `as_string()` + ## v0.7.3 - 2023-04-10 - Added `@glimpse()` diff --git a/Project.toml b/Project.toml index 937a8d5..697ef26 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Tidier" uuid = "f0413319-3358-4bb0-8e7c-0c83523a93bd" authors = ["Karandeep Singh"] -version = "0.7.3" +version = "0.7.4" [deps] Chain = "8be319e6-bccf-4806-a6f7-6fae938471bc" diff --git a/README.md b/README.md index cfc5842..47cb405 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,7 @@ Tidier.jl also supports the following helper functions: - `ntile()` - `lag()` and `lead()` - `starts_with()`, `ends_with()`, `matches()`, and `contains()` +- `as_float()`, `as_integer()`, and `as_string()` See the documentation [Home](https://tidierorg.github.io/Tidier.jl/dev/) page for a guide on how to get started, or the [Reference](https://tidierorg.github.io/Tidier.jl/dev/reference/) page for a detailed guide to each of the macros and functions. diff --git a/docs/src/index.md b/docs/src/index.md index 5469e7d..fbcd68c 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -115,6 +115,7 @@ Tidier.jl also supports the following helper functions: - `ntile()` - `lag()` and `lead()` - `starts_with()`, `ends_with()`, `matches()`, and `contains()` + - `as_float()`, `as_integer()`, and `as_string()` ``` See the [Reference](https://tidierorg.github.io/Tidier.jl/dev/reference/) page for a detailed guide to each of the macros and functions. diff --git a/src/Tidier.jl b/src/Tidier.jl index f648ae3..a762731 100644 --- a/src/Tidier.jl +++ b/src/Tidier.jl @@ -15,9 +15,9 @@ using Reexport @reexport using ShiftedArrays: lag, lead export Tidier_set, across, desc, n, row_number, starts_with, ends_with, matches, if_else, case_when, ntile, - @select, @transmute, @rename, @mutate, @summarize, @summarise, @filter, @group_by, @ungroup, @slice, - @arrange, @distinct, @pull, @left_join, @right_join, @inner_join, @full_join, @pivot_wider, @pivot_longer, - @bind_rows, @bind_cols, @clean_names, @count, @tally, @drop_na, @glimpse + as_float, as_integer, as_string, @select, @transmute, @rename, @mutate, @summarize, @summarise, @filter, + @group_by, @ungroup, @slice, @arrange, @distinct, @pull, @left_join, @right_join, @inner_join, @full_join, + @pivot_wider, @pivot_longer, @bind_rows, @bind_cols, @clean_names, @count, @tally, @drop_na, @glimpse # Package global variables const code = Ref{Bool}(false) # output DataFrames.jl code? @@ -35,6 +35,7 @@ include("conditionals.jl") include("pseudofunctions.jl") include("helperfunctions.jl") include("ntile.jl") +include("type_conversions.jl") # Function to set global variables """ diff --git a/src/docstrings.jl b/src/docstrings.jl index 8ed68ea..e7d18e7 100644 --- a/src/docstrings.jl +++ b/src/docstrings.jl @@ -1869,4 +1869,82 @@ Groups: a [100] .b Int64 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, .c String a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, ``` +""" + +const docstring_as_float = +""" + as_float(value) + +Convert a number or string to a Float64 data type. + +This is a useful helper for type conversions. Missing values are propagated. + +# Arguments +- `value`: An `AbstractString`, `Number`, or `missing` value. + +# Examples +```jldoctest +julia> as_float(1) +1.0 + +julia> as_float("1.5") +1.5 + +julia> as_float(missing) +missing +``` +""" + +const docstring_as_integer = +""" + as_integer(value) + +Convert a number or string to an Int64 data type. + +This is a useful helper for type conversions. Missing values are propagated. Any values after the decimal point are removed. + +# Arguments +- `value`: An `AbstractString`, `Number`, or `missing` value. + +# Examples +```jldoctest +julia> as_integer(1) +1 + +julia> as_integer(1.5) +1 + +julia> as_integer("2") +2 + +julia> as_integer("2.5") +2 + +julia> as_integer(missing) +missing +``` +""" + +const docstring_as_string = +""" + as_string(value) + +Convert a number or string to a String data type. + +This is a useful helper for type conversions. Missing values are propagated. + +# Arguments +- `value`: An `AbstractString`, `Number`, or `missing` value. + +# Examples +```jldoctest +julia> as_string(1) +"1" + +julia> as_string(1.5) +"1.5" + +julia> as_string(missing) +missing +``` """ \ No newline at end of file diff --git a/src/type_conversions.jl b/src/type_conversions.jl new file mode 100644 index 0000000..d77f402 --- /dev/null +++ b/src/type_conversions.jl @@ -0,0 +1,47 @@ +""" +$docstring_as_float +""" +function as_float(value) + try + passmissing(convert)(Float64, value) + catch + missing # if parsing failure + end +end + +function as_float(value::AbstractString) + try + passmissing(parse)(Float64, value) + catch + missing # if parsing failure + end +end + +""" +$docstring_as_integer +""" +function as_integer(value) + try + passmissing(floor)(value) |> + x -> passmissing(convert)(Int64, x) + catch + missing # if parsing failure + end +end + +function as_integer(value::AbstractString) + try + passmissing(parse)(Float64, value) |> + x -> passmissing(floor)(x) |> + x -> passmissing(convert)(Int64, x) + catch + missing # if parsing failure + end +end + +""" +$docstring_as_string +""" +function as_string(value) + passmissing(string)(value) +end \ No newline at end of file