# How to replace/set/change value in array/dataframe if value is less/greater than?

**URL:** <https://discourse.julialang.org/t/how-to-replace-set-change-value-in-array-dataframe-if-value-is-less-greater-than/51039>\
**Category:** New to Julia\
**Created:** [December 1, 2020, 10:23am UTC](https://discourse.julialang.org/t/how-to-replace-set-change-value-in-array-dataframe-if-value-is-less-greater-than/51039 "2020-12-01T10:23:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [December 1, 2020, 10:23am UTC](https://discourse.julialang.org/t/how-to-replace-set-change-value-in-array-dataframe-if-value-is-less-greater-than/51039/1 "2020-12-01T10:23:49Z")

</div>

Hello, I had some code that used to work but it must have been for a previous version of Julia. I can’t find what I am looking for in the DataFrame docs [Getting Started · DataFrames.jl](https://dataframes.juliadata.org/stable/man/getting_started/#Replacing-Data)

I want to replace values in an array or DataFrame that are less than 0.5 to 0.0.

```julia
QWER = rand(5, 5)
QWER[QWER .>= 0.5] .= 0.0

```

This gives a 12 element view, everything is set to 0.0.

```julia
QWER = [QWER .<= 0.5] .= 0.0

```

This gives the error  
`MethodError: Cannot`convert` an object of type Float64 to an object of type BitArray{2}`

```julia
using DataFrames
QWER = DataFrame(QWER)
QWER = [QWER .<= 0.5] .= 0.0

```

This gives the error  
`MethodError: Cannot `convert` an object of type Float64 to an object of type BitArray{2}`

```julia
QWER[QWER .>= 0.5] .= 0.0 

```

This gives the error  
`MethodError: no method matching getindex(::DataFrame, ::DataFrame)`

What is the correct way to do this?  
#########  
Answer edit:

> [@Rudi79](#):
>
> For Arrays the first solution is correct. Just look at QWER.

For a DataFrame

> [@Rudi79](#):
>
> `QWER .= ifelse.(QWER .> 0.5, 0.0, QWER)`

---

<div class="post-metadata">

**Author:** ![Rudi79](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rudi79/32/3884_2.png) [@Rudi79](https://discourse.julialang.org/u/Rudi79)\
**Post date:** [December 1, 2020, 10:33am UTC](https://discourse.julialang.org/t/how-to-replace-set-change-value-in-array-dataframe-if-value-is-less-greater-than/51039/2 "2020-12-01T10:33:35Z")

</div>

For Arrays the first solution is correct. Just look at QWER.  
For DataFrames something like

```julia
df[df.col1 .> 0.5,:col1] .= 0.0

```

should do the trick

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [December 1, 2020, 10:38am UTC](https://discourse.julialang.org/t/how-to-replace-set-change-value-in-array-dataframe-if-value-is-less-greater-than/51039/3 "2020-12-01T10:38:10Z")

</div>

> [@Rudi79](#):
>
> `df[df.col1 .> 0.5,:col1] .= 0.0`

Thank you for your reply - you are correct, the arrays solution works.

Does the DataFrames on not just specify the first column (assuming it is named col1)?

---

<div class="post-metadata">

**Author:** ![Rudi79](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rudi79/32/3884_2.png) [@Rudi79](https://discourse.julialang.org/u/Rudi79)\
**Post date:** [December 1, 2020, 10:53am UTC](https://discourse.julialang.org/t/how-to-replace-set-change-value-in-array-dataframe-if-value-is-less-greater-than/51039/4 "2020-12-01T10:53:51Z")

</div>

Yes, if you want to change all of the columns, then use

```julia
QWER .= ifelse.(QWER .> 0.5, 0.0, QWER)

```
