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

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:

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:

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:

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

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)]

1 Like

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?

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

2 Likes

Thanks a lot!

You can also just do map

df.new_col = map(eachrow(df)) do row
    row.x + row.y
end
1 Like

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.

1 Like

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.

1 Like