# Is there something similar to R's pmap in Julia?

**URL:** <https://discourse.julialang.org/t/is-there-something-similar-to-rs-pmap-in-julia/37055>\
**Category:** New to Julia\
**Tags:** question, pmap\
**Created:** [April 5, 2020, 12:14pm UTC](https://discourse.julialang.org/t/is-there-something-similar-to-rs-pmap-in-julia/37055 "2020-04-05T12:14:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ameresv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ameresv/32/14372_2.png) [@ameresv](https://discourse.julialang.org/u/ameresv)\
**Post date:** [April 5, 2020, 12:14pm UTC](https://discourse.julialang.org/t/is-there-something-similar-to-rs-pmap-in-julia/37055/1 "2020-04-05T12:14:15Z")

</div>

Hi! I am new with Julia (being an R-guy) I have a question:  
Is something similar to pmap here in Julia? Why I am asking this, because I have this dataframe:

```julia
m = DataFrame(
    x = [1,2,3],
    y = [[1,2,3],[4,5,6],[7,8,9]],
    z = [[4,3,2],[6,5,4],[9,8,7]]
)

```

My objective is to get a “b-columns” that will result of adding up each “y and z” array with the x-string. Let me put this in numbers for better understanding:

```julia
m = DataFrame(
    x = [1,2,3],
    y = [[1,2,3],[4,5,6],[7,8,9]],
    z = [[4,3,2],[6,5,4],[9,8,7]],
    b = [[6,6,6],[12,12,12],[19,19,19]]
)

```

I can do it in R using pmap following this:

```julia
m <- m %>% 
 mutate(b = pmap(list(x,y,z), 
                 ~pmap_dbl(list(..1,..2,..3), ~ (..1 + ..2 + ..3))))

```

Is there a similar way to do it in Julia?  
Regards

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [April 5, 2020, 12:38pm UTC](https://discourse.julialang.org/t/is-there-something-similar-to-rs-pmap-in-julia/37055/2 "2020-04-05T12:38:01Z")

</div>

Most `map` and `apply` related R-things should probably expressed as broadcasted calls in Julia. For your use case this might be the cleanest way:

`m.b = [r.z .+ r.x .+ r.y for r ∈ eachrow(m)] `

---

<div class="post-metadata">

**Author:** ![ameresv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ameresv/32/14372_2.png) [@ameresv](https://discourse.julialang.org/u/ameresv)\
**Post date:** [April 5, 2020, 1:06pm UTC](https://discourse.julialang.org/t/is-there-something-similar-to-rs-pmap-in-julia/37055/3 "2020-04-05T13:06:17Z")

</div>

This “for r ∈ eachrow(m)” piece simply blew my mind.  
I didn’t imagine that could be so simple.  
Thanks a lot for the answer. However, this broadcast call could be applied in predefined functions? e.g if I have a function called “sum\_squared”, I can be broadcasted like .sum\_squared?

---

<div class="post-metadata">

**Author:** ![bashonubuntu](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bashonubuntu](https://discourse.julialang.org/u/bashonubuntu)\
**Post date:** [April 5, 2020, 1:25pm UTC](https://discourse.julialang.org/t/is-there-something-similar-to-rs-pmap-in-julia/37055/4 "2020-04-05T13:25:23Z")

</div>

Yes, you can. For a user-defined function `f(x)`, the broadcast analog is simply `f.(x)`.

---

<div class="post-metadata">

**Author:** ![ameresv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ameresv/32/14372_2.png) [@ameresv](https://discourse.julialang.org/u/ameresv)\
**Post date:** [April 5, 2020, 3:25pm UTC](https://discourse.julialang.org/t/is-there-something-similar-to-rs-pmap-in-julia/37055/5 "2020-04-05T15:25:43Z")

</div>

Thanks a lot!

---

<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:** [April 5, 2020, 4:37pm UTC](https://discourse.julialang.org/t/is-there-something-similar-to-rs-pmap-in-julia/37055/6 "2020-04-05T16:37:50Z")

</div>

You can also just do `map`

```julia
df.new_col = map(eachrow(df)) do row
    row.x + row.y
end

```

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [April 5, 2020, 9:05pm UTC](https://discourse.julialang.org/t/is-there-something-similar-to-rs-pmap-in-julia/37055/7 "2020-04-05T21:05:38Z")

</div>

> [@nilshg](#):
>
> Most `map` and `apply` related R-things should probably expressed as broadcasted calls in Julia.

I don’t know that I agree with this. Yes, broadcasting is a super convenient syntax assuming you’re really confident in what things act as scalars and which do not. I’ve had some really weird results that were difficult for me to diagnose because I failed to wrap the right things in `Ref`. If you’re used to `map()` and `apply()` in another language `map()` in Julia works great.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [April 6, 2020, 6:22am UTC](https://discourse.julialang.org/t/is-there-something-similar-to-rs-pmap-in-julia/37055/8 "2020-04-06T06:22:39Z")

</div>

Yes sorry “should be” it’s probably a bit of a strong way of putting it - “can be” would have been better, or maybe one could say that using the broadcasting syntax is in most cases the idiomatic choice.
