Skip to content

Instantly share code, notes, and snippets.

@genkuroki
Last active May 17, 2021 03:22
Show Gist options
  • Save genkuroki/294a5b53b1b15ba605757168fb613825 to your computer and use it in GitHub Desktop.
Save genkuroki/294a5b53b1b15ba605757168fb613825 to your computer and use it in GitHub Desktop.
convert String NA to missing
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "data = string.(randn(10))\ndata[[5, 6, 9]] .= \"NA\"\ndata",
"execution_count": 1,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 1,
"data": {
"text/plain": "10-element Vector{String}:\n \"1.0106259603882495\"\n \"0.8199597254270726\"\n \"0.38521766934693924\"\n \"1.0373421561430833\"\n \"NA\"\n \"NA\"\n \"-0.5257336846462347\"\n \"-0.7228884135550121\"\n \"NA\"\n \"-0.30662288048563063\""
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = []\nfor x in data\n if x == \"NA\"\n push!(a, missing)\n else\n push!(a, parse(Float64, x))\n end\nend\na",
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 2,
"data": {
"text/plain": "10-element Vector{Any}:\n 1.0106259603882495\n 0.8199597254270726\n 0.38521766934693924\n 1.0373421561430833\n missing\n missing\n -0.5257336846462347\n -0.7228884135550121\n missing\n -0.30662288048563063"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = Float64[]\nfor x in data\n if x == \"NA\"\n push!(a, missing)\n else\n push!(a, parse(Float64, x))\n end\n @show a\nend\na",
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": "a = [1.0106259603882495]\na = [1.0106259603882495, 0.8199597254270726]\na = [1.0106259603882495, 0.8199597254270726, 0.38521766934693924]\na = [1.0106259603882495, 0.8199597254270726, 0.38521766934693924, 1.0373421561430833]\n",
"name": "stdout"
},
{
"output_type": "error",
"ename": "LoadError",
"evalue": "MethodError: \u001b[0mCannot `convert` an object of type \u001b[92mMissing\u001b[39m\u001b[0m to an object of type \u001b[91mFloat64\u001b[39m\n\u001b[0mClosest candidates are:\n\u001b[0m convert(::Type{T}, \u001b[91m::T\u001b[39m) where T<:Number at number.jl:6\n\u001b[0m convert(::Type{T}, \u001b[91m::Number\u001b[39m) where T<:Number at number.jl:7\n\u001b[0m convert(::Type{T}, \u001b[91m::Base.TwicePrecision\u001b[39m) where T<:Number at twiceprecision.jl:250\n\u001b[0m ...",
"traceback": [
"MethodError: \u001b[0mCannot `convert` an object of type \u001b[92mMissing\u001b[39m\u001b[0m to an object of type \u001b[91mFloat64\u001b[39m\n\u001b[0mClosest candidates are:\n\u001b[0m convert(::Type{T}, \u001b[91m::T\u001b[39m) where T<:Number at number.jl:6\n\u001b[0m convert(::Type{T}, \u001b[91m::Number\u001b[39m) where T<:Number at number.jl:7\n\u001b[0m convert(::Type{T}, \u001b[91m::Base.TwicePrecision\u001b[39m) where T<:Number at twiceprecision.jl:250\n\u001b[0m ...",
"",
"Stacktrace:",
" [1] push!(a::Vector{Float64}, item::Missing)",
" @ Base .\\array.jl:966",
" [2] top-level scope",
" @ .\\In[3]:4",
" [3] eval",
" @ .\\boot.jl:369 [inlined]",
" [4] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)",
" @ Base .\\loading.jl:1097"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = Union{Missing, Float64}[]\nfor x in data\n if x == \"NA\"\n push!(a, missing)\n else\n push!(a, parse(Float64, x))\n end\nend\na",
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": "10-element Vector{Union{Missing, Float64}}:\n 1.0106259603882495\n 0.8199597254270726\n 0.38521766934693924\n 1.0373421561430833\n missing\n missing\n -0.5257336846462347\n -0.7228884135550121\n missing\n -0.30662288048563063"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "f(x) = x == \"NA\" ? missing : parse(Float64, x)\na = f.(data)\na",
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 5,
"data": {
"text/plain": "10-element Vector{Union{Missing, Float64}}:\n 1.0106259603882495\n 0.8199597254270726\n 0.38521766934693924\n 1.0373421561430833\n missing\n missing\n -0.5257336846462347\n -0.7228884135550121\n missing\n -0.30662288048563063"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "julia-1.7-depwarn-o3",
"display_name": "Julia 1.7.0-DEV depwarn -O3",
"language": "julia"
},
"language_info": {
"file_extension": ".jl",
"name": "julia",
"mimetype": "application/julia",
"version": "1.7.0"
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"base_numbering": 1,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"@webio": {
"lastKernelId": null,
"lastCommId": null
},
"gist": {
"id": "294a5b53b1b15ba605757168fb613825",
"data": {
"description": "convert String NA to missing",
"public": true
}
},
"_draft": {
"nbviewer_url": "https://gist.github.com/294a5b53b1b15ba605757168fb613825"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment