# Transform counts to binary variable in DataFrame

**URL:** <https://discourse.julialang.org/t/transform-counts-to-binary-variable-in-dataframe/69044>\
**Category:** General Usage\
**Tags:** dataframes, data\_structures\
**Created:** [October 1, 2021, 1:21pm UTC](https://discourse.julialang.org/t/transform-counts-to-binary-variable-in-dataframe/69044 "2021-10-01T13:21:25Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 1, 2021, 3:04pm UTC](https://discourse.julialang.org/t/transform-counts-to-binary-variable-in-dataframe/69044/5 "2021-10-01T15:04:44Z")

</div>

One more approach, inspired by [this post](https://discourse.julialang.org/t/repeating-with-function-repeat-array-elements-using-a-vector-of-numbers/21435/2):

```julia
v1 = vcat(fill.(df1.subject, df1.count)...)
v2 = vcat(fill.(df1.response, df1.count)...) .== "yes"
df2 = DataFrame(subject = v1, y = Int64.(v2))

5×2 DataFrame
 Row │ subject y     
     │ Int64 Int64 
─────┼────────────────
   1 │ 1 1
   2 │ 1 1
   3 │ 2 1
   4 │ 2 1
   5 │ 2 0

```

---

_[View the full topic](https://discourse.julialang.org/t/transform-counts-to-binary-variable-in-dataframe/69044)._
