PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, October 8, 2022

[FIXED] How to prop.table() in julia

 October 08, 2022     dataframe, julia, statistics     No comments   

Issue

I am trying to move from R to Julia.

So I have a dataset with 2 columns of prices and 2 conditional columns telling me if the price is "cheap" or "expensive".

So I want to count how many "cheap" or "expensive" entries are.

So using the package DataStructures I got this:

using DataStructures
counter(df.p_orellana)

Accumulator{Union{Missing, String}, Int64} with 3 entries:
  "expensive"   => 18
  missing  => 2
  "cheap" => 22

This would be the same as the table() function in R.

Is there any way to make these values proportions?

In R it would be to prop.Table() function, but I am not sure how to do it with Julia.

I would like to have:

Accumulator{Union{Missing, String}, Int64} with 3 entries:
  "expensive"   => 0.4285
  missing  => 0.0476
  "cheap" => 0.5238

Thanks in advance!


Solution

Use the FreqTables.jl package.

Here is an example:

julia> using FreqTables

julia> data = [fill("expensive", 18); fill(missing, 2); fill("cheap", 22)];

julia> freqtable(data)
3-element Named Vector{Int64}
Dim1      │
──────────┼───
cheap     │ 22
expensive │ 18
missing   │  2

julia> proptable(data)
3-element Named Vector{Float64}
Dim1      │
──────────┼─────────
cheap     │  0.52381
expensive │ 0.428571
missing   │ 0.047619

The results are shown in sorted order. If you would like other order use the CategoricalArrays.jl package additionally and set an appropriate ordering of levels:

julia> using CategoricalArrays

julia> cat_data = categorical(data, levels=["expensive", "cheap"]);

julia> freqtable(cat_data)
3-element Named Vector{Int64}
Dim1        │
────────────┼───
"expensive" │ 18
"cheap"     │ 22
missing     │  2

julia> proptable(cat_data)
3-element Named Vector{Float64}
Dim1        │
────────────┼─────────
"expensive" │ 0.428571
"cheap"     │  0.52381
missing     │ 0.047619


Answered By - Bogumił Kamiński
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing