# Import numerical data from text file, sort and retain only Floats

**URL:** <https://discourse.julialang.org/t/import-numerical-data-from-text-file-sort-and-retain-only-floats/68216>\
**Category:** General Usage\
**Tags:** csv, import-data\
**Created:** [September 15, 2021, 4:26pm UTC](https://discourse.julialang.org/t/import-numerical-data-from-text-file-sort-and-retain-only-floats/68216 "2021-09-15T16:26:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dalarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dalarev/32/9881_2.png) [@dalarev](https://discourse.julialang.org/u/dalarev)\
**Post date:** [September 15, 2021, 4:26pm UTC](https://discourse.julialang.org/t/import-numerical-data-from-text-file-sort-and-retain-only-floats/68216/1 "2021-09-15T16:26:47Z")

</div>

**MWE:**

`readdlm` interprets data imported form a .dat text file as:

```julia
julia> A 
2×4 Matrix{Any}:
 1 2 "" 3
 4 5 6 ""

```

I would like to ignore the `""` `SubString{String}` and raster by row to obtain the following final vector:

```julia
6-element Vector{Float64}:
 1
 2
 3
 4
 5
 6

```

**Things I’ve Tried:**

Transpose `'` of the data won’t work because `typeof(A)` is `Matrix{Any}`.

`deleteat!(data .== "")` also won’t work for the same reason.

I also tried creating a new array `B` containing only the Floats of `A`, but that didn’t work because it rasters by columns, even if I initialize `B` as a matrix:

```julia
ind = findall(A .!= "")
B = Inf .* ones(size(A))

julia> B[ind] = A[ind]
6-element Vector{Any}:
 1
 4
 2
 5
 6
 3

```

**Requesting Help**

It was suggested on Slack that I use CSV.jl. I am trying that now, but I am new to CSV and DataFrames, so it is going slowly.

Appreciate any insight the community is able to offer!

---

<div class="post-metadata">

**Author:** ![dalarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dalarev/32/9881_2.png) [@dalarev](https://discourse.julialang.org/u/dalarev)\
**Post date:** [September 15, 2021, 4:55pm UTC](https://discourse.julialang.org/t/import-numerical-data-from-text-file-sort-and-retain-only-floats/68216/2 "2021-09-15T16:55:19Z")

</div>

I think I found a way! But I’m sure this can be made more elegant/efficient.

Here’s what worked for me:

```julia
A[findall(A .== "")] .= Inf

B = A'[:]

julia> deleteat!(B,B .== Inf)
6-element Vector{Any}:
 1
 2
 3
 4
 5
 6

```

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [September 15, 2021, 5:05pm UTC](https://discourse.julialang.org/t/import-numerical-data-from-text-file-sort-and-retain-only-floats/68216/3 "2021-09-15T17:05:34Z")

</div>

```julia
using DelimitedFiles

x = readdlm("datafile.txt")
#=
2×4 Matrix{Any}:
 1 2 "" 3
 4 5 6 ""
=#

# the use of `permutedims` is from Jeff_Emanuel's entry (below)
result = filter(!isempty, permutedims(x))
#=
6-element Vector{Any}:
 1
 2
 3
 4
 5
 6
=#

```

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [September 15, 2021, 5:11pm UTC](https://discourse.julialang.org/t/import-numerical-data-from-text-file-sort-and-retain-only-floats/68216/4 "2021-09-15T17:11:29Z")

</div>

In this answer, the order of the numbers is different. It should be `1 2 3 ...` That’s kind of the tricky part of the problem… 😕

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [September 15, 2021, 5:15pm UTC](https://discourse.julialang.org/t/import-numerical-data-from-text-file-sort-and-retain-only-floats/68216/5 "2021-09-15T17:15:13Z")

</div>

```julia
julia> x=[1 2 "" 3; 4 5 6 ""]
2×4 Matrix{Any}:
 1 2 "" 3
 4 5 6 ""

julia> filter(!isempty, permutedims(x))
6-element Vector{Any}:
 1
 2
 3
 4
 5
 6

```

---

<div class="post-metadata">

**Author:** ![dalarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dalarev/32/9881_2.png) [@dalarev](https://discourse.julialang.org/u/dalarev)\
**Post date:** [September 15, 2021, 6:19pm UTC](https://discourse.julialang.org/t/import-numerical-data-from-text-file-sort-and-retain-only-floats/68216/6 "2021-09-15T18:19:18Z")

</div>

Nice! This does it!

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 15, 2021, 8:22pm UTC](https://discourse.julialang.org/t/import-numerical-data-from-text-file-sort-and-retain-only-floats/68216/7 "2021-09-15T20:22:53Z")

</div>

To explain why this works (as it may not be obvious): in julia, numbers are iterable and have a length of 1. Thus, `isempty(3)` is false and it doesn’t get filtered out, in contrast to `isempty("")`, which is true and gets removed.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [September 15, 2021, 10:09pm UTC](https://discourse.julialang.org/t/import-numerical-data-from-text-file-sort-and-retain-only-floats/68216/8 "2021-09-15T22:09:15Z")

</div>

In case it might interest, similar result can be obtained with:  
`permutedims(A)[isa.(A, Int)']`
