Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter rows from Julia Array based on value of value in specified column?

Tags:

filter

julia

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.)

like image 980
Luke Avatar asked Oct 08 '16 20:10

Luke


People also ask

How do I filter for specific rows in Julia?

The first filtering section demonstrates how you can use one condition or multiple conditions to filter for specific rows. In Julia, you precede the comparison operator with a period, for example .==, to do element-wise comparisons. To use multiple conditions you wrap each one in parentheses and combine them with ANDs, &, or ORs, |.

How to filter rows by list of values in Excel?

Filter rows by list of values with Advanced Filter feature. We can apply the Advanced Filter feature to filter rows by a given list of values easily in Excel. Please do as follows: 1. Click Data > Advanced to open the Advanced Filter dialog box. 2.

What is the input value of array3 in Julia?

Input : array3 = [ 22, 7, 91, 69, 2, 74 ] sch = 6 Output : Element not found. [Note: Unlike other languages, In Julia indexing of an Array starts with 1.]

How to find all occurrences of an array in Julia?

[Note: Unlike other languages, In Julia indexing of an Array starts with 1.] The findall () method is used to find all the occurrences of the required element from an array, and return an Array (or CartesianArray, depending on the dimensions of input array) of the index, where the elements are present in the input array.


2 Answers

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     │ "..." │ "..." │ "..." │
like image 54
mbauman Avatar answered Oct 13 '22 05:10

mbauman


data[find(x -> a[x,1] == 1, 1:size(data)[1]),:]
like image 30
isebarn Avatar answered Oct 13 '22 05:10

isebarn