# Change column to row using conditions

**URL:** <https://discourse.julialang.org/t/change-column-to-row-using-conditions/83175>\
**Category:** New to Julia\
**Tags:** dataframes\
**Created:** [June 22, 2022, 9:53am UTC](https://discourse.julialang.org/t/change-column-to-row-using-conditions/83175 "2022-06-22T09:53:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sai\_matcha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sai_matcha/32/35801_2.png) [@sai\_matcha](https://discourse.julialang.org/u/sai_matcha)\
**Post date:** [June 22, 2022, 9:53am UTC](https://discourse.julialang.org/t/change-column-to-row-using-conditions/83175/1 "2022-06-22T09:53:23Z")

</div>

Hi,  
i have a dataframe like this

```julia
df = DataFrame()

df.id = sort!(repeat(1:3,5))
df.time = repeat(1:5,3)
df.dv1 = repeat(['.',".",".",".",1],3)
df.dv2 = repeat(['.',".",".",".",10],3)
df.cmt = repeat([1,1,1,1,2],3)

```

i just want to delete dv2 column, and insert non missing dv2 values in dv1 one column wherever time and id are matching. and change the df.cmt to 3 wherever df2 values were inserted. Here is the DataFrame i want.

```julia
df2 = DataFrame()
df2.id = sort!(repeat(1:3,6))
df2.time = repeat([1,2,3,4,5,5],3)
df2.dv = repeat(['.',".",".",".",1,10],3)
df2.cmt = repeat([1,1,1,1,2,3],3)

```

Hope i am clear. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [June 22, 2022, 3:47pm UTC](https://discourse.julialang.org/t/change-column-to-row-using-conditions/83175/2 "2022-06-22T15:47:49Z")

</div>

This gives you a different row order, but I assume this is acceptable (you can always re-order later):

```julia
dv2 = df.dv2;
df2 = select(df, Not(:dv2));
for (i, v) in enumerate(dv2)
    if v isa Number
        push!(df2, (df2.id[i], df2.time[i], v, 3))
    end
end

```

Note that your data does not have missing values but a mixture of `'.'` and `"."` for non-numeric data so I did `v isa Number` check. You might want to use some other test in your production code.

---

<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:** [June 22, 2022, 4:22pm UTC](https://discourse.julialang.org/t/change-column-to-row-using-conditions/83175/3 "2022-06-22T16:22:36Z")

</div>

OP, it seems you may be coming from Stata, where missing values appear as `.` . Julia does not work this way. We have a type `missing` which is used for missing values. I encourage you to read that instead.

One way to do this easily with DataFramesMeta.jl is with the `@eachrow` macro

```julia
julia> @eachrow df begin 
           if :time == :id
               if !ismissing(:dv2)
                   :dv1 = :dv2
                   :cmt = 3
               end
           end
       end

```

Although I followed your instructions in your post and no row matches the criteria for changes.

---

<div class="post-metadata">

**Author:** ![sai\_matcha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sai_matcha/32/35801_2.png) [@sai\_matcha](https://discourse.julialang.org/u/sai_matcha)\
**Post date:** [June 23, 2022, 5:01am UTC](https://discourse.julialang.org/t/change-column-to-row-using-conditions/83175/4 "2022-06-23T05:01:40Z")

</div>

Sorry, I meant id and time values are same for dv1 and dv2

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [June 23, 2022, 6:58am UTC](https://discourse.julialang.org/t/change-column-to-row-using-conditions/83175/5 "2022-06-23T06:58:44Z")

</div>

@pdeffebach - OP wanted to duplicate rows in which `:dv2` was non-missing.
