# Julia Beginner: How to create grouped variable in julia

**URL:** <https://discourse.julialang.org/t/julia-beginner-how-to-create-grouped-variable-in-julia/66547>\
**Category:** New to Julia\
**Created:** [August 17, 2021, 1:06pm UTC](https://discourse.julialang.org/t/julia-beginner-how-to-create-grouped-variable-in-julia/66547 "2021-08-17T13:06:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![atplf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atplf/32/28254_2.png) [@atplf](https://discourse.julialang.org/u/atplf)\
**Post date:** [August 17, 2021, 1:06pm UTC](https://discourse.julialang.org/t/julia-beginner-how-to-create-grouped-variable-in-julia/66547/1 "2021-08-17T13:06:06Z")

</div>

I have a column of age values from dataset that I need to convert to age ranges of 18-29, 30-39, 40-49, 50-59, 60-69, and 70+:  
and i want it like this: ![lLuAG (1)](https://global.discourse-cdn.com/julialang/original/3X/1/9/19bbdc25a757fb3b29b408345630d96f33f70721.png)  
Anyone can help me. Thanks

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [August 17, 2021, 1:12pm UTC](https://discourse.julialang.org/t/julia-beginner-how-to-create-grouped-variable-in-julia/66547/2 "2021-08-17T13:12:56Z")

</div>

The easiest way to do this is `map` with a bunch of `if-else` conditions.

```julia
julia> df = DataFrame(age = [81, 42, 18, 55, 23, 35]);

julia> function bin_age(age)
           if age > 70 
               "70+"
           elseif age > 60  
               "60-69"
           elseif age > 50
               "50-59"
           elseif age > 40
               "40-49"
           elseif age > 30
               "30-39"
           elseif age > 20
               "20-29"
           elseif age > 10
               "10-19"
           end
       end;

julia> df.agerange = bin_age.(df.age);

julia> df
6×2 DataFrame
 Row │ age agerange 
     │ Int64 String   
─────┼─────────────────
   1 │ 81 70+
   2 │ 42 40-49
   3 │ 18 10-19
   4 │ 55 50-59
   5 │ 23 20-29
   6 │ 35 30-39

```

---

<div class="post-metadata">

**Author:** ![atplf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atplf/32/28254_2.png) [@atplf](https://discourse.julialang.org/u/atplf)\
**Post date:** [August 17, 2021, 1:18pm UTC](https://discourse.julialang.org/t/julia-beginner-how-to-create-grouped-variable-in-julia/66547/3 "2021-08-17T13:18:29Z")

</div>

thankyou so much, this way is very useful for me

---

<div class="post-metadata">

**Author:** ![tshort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tshort/32/43_2.png) [@tshort](https://discourse.julialang.org/u/tshort)\
**Post date:** [August 17, 2021, 1:45pm UTC](https://discourse.julialang.org/t/julia-beginner-how-to-create-grouped-variable-in-julia/66547/4 "2021-08-17T13:45:51Z")

</div>

For more general binning, see:

> **[Binning your data with Julia](https://bkamins.github.io/julialang/2020/12/11/binning.html)**
>
> Introduction
