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

MWE:

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

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:

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:

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!

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

Here’s what worked for me:

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

B = A'[:]

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

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
=#

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… :confused:

1 Like
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
4 Likes

Nice! This does it!

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.

2 Likes

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

1 Like