7

I have data like this in a text file:

CLASS     col2    col3    ...
1         ...     ...     ...
1         ...     ...     ...
2         ...     ...     ...
2         ...     ...     ...
2         ...     ...     ...

I load them using the following code:

data = readdlm("file.txt")[2:end, :] # without header line

And now I would like to get array with rows only from class 1.

(Data could be loaded using some other function if it would help.)

1

2 Answers 2

11

Logical indexing is the straight-forward way to do filtering on an array:

data[data[:,1] .== 1, :]

If, though, you read your file in as a data frame, you'll have more options available to you, and it'll keep track of your headers:

julia> using DataFrames
julia> df = readtable("file.txt", separator=' ')
5×4 DataFrames.DataFrame
│ Row │ CLASS │ col2  │ col3  │ _     │
├─────┼───────┼───────┼───────┼───────┤
│ 1   │ 1     │ "..." │ "..." │ "..." │
│ 2   │ 1     │ "..." │ "..." │ "..." │
│ 3   │ 2     │ "..." │ "..." │ "..." │
│ 4   │ 2     │ "..." │ "..." │ "..." │
│ 5   │ 2     │ "..." │ "..." │ "..." │

julia> df[df[:CLASS] .== 1, :] # Refer to the column by its header name
2×4 DataFrames.DataFrame
│ Row │ CLASS │ col2  │ col3  │ _     │
├─────┼───────┼───────┼───────┼───────┤
│ 1   │ 1     │ "..." │ "..." │ "..." │
│ 2   │ 1     │ "..." │ "..." │ "..." │

There are even more tools available with the DataFramesMeta package that aim to make this simpler (and other packages actively under development). You can use its @where macro to do SQL-style filtering:

julia> using DataFramesMeta
julia> @where(df, :CLASS .== 1)
2×4 DataFrames.DataFrame
│ Row │ CLASS │ col2  │ col3  │ _     │
├─────┼───────┼───────┼───────┼───────┤
│ 1   │ 1     │ "..." │ "..." │ "..." │
│ 2   │ 1     │ "..." │ "..." │ "..." │
1
  • For me df[df[:CLASS] .== 1, :] gave an error, but df[df[!, :CLASS] .== 1, :] worked.
    – sn0b4ll
    Commented Nov 13, 2022 at 17:03
4
data[find(x -> a[x,1] == 1, 1:size(data)[1]),:]
5
  • 6
    No need to make it so convoluted. Just data[data[:,1] .== 1, :] should do the trick.
    – mbauman
    Commented Oct 8, 2016 at 23:55
  • Oh jesus I was trying to do that for so long, forgot to do the : at the end
    – isebarn
    Commented Oct 9, 2016 at 0:30
  • @MattB. You could write it as a separate answer. ;)
    – Luke
    Commented Oct 21, 2016 at 15:04
  • @Luke done. I figured it could be a simple edit for @isebarn, but I've added a few more options in my answer.
    – mbauman
    Commented Oct 23, 2016 at 17:35
  • I thought it was "stealing" to edit my response to your answer
    – isebarn
    Commented Oct 23, 2016 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.