About rowwise and do

Hi! I have a question: I have made a dataframe as follows:

library(tidyverse)
df = tibble(x = c(1,2),
            y = list(1:3,4:6))

In order to obtain the z column - which is the sum of x and each y^2 - I have done this:

df <- df %>% 
 mutate(z = map2(x,y, ~map2_dbl(.x,.y, ~ (.x + .y^2))))

As you can see, is a bit strange chunk of code, but it works. Now, doing so with rowwise, however, I obtain the following:

df$z2 <- df %>% 
 rowwise() %>% 
 do(z2 = .$x +.$y^2)

My main concern is that I cannot have the columns as I obtained by mapping them. (A z2$z2 column is obtained with the last chunk)
Any suggestion, please?
Regards

library(tidyverse)
df <- tibble(
  x = c(1, 2),
  y = list(1:3, 4:6)
)
# In order to obtain the z column - which is the sum of x and each y^2 - I have done this:

df <- df %>%
  mutate(z = map2(x, y, ~ map2_dbl(.x, .y, ~ (.x + .y^2))))

#for z2 added to df
df <- bind_cols(df, {
  rowwise(df) %>% do(z2 = .$x + .$y^2)
})
1 Like

Hi! thanks for your answer. Relating to it, how can I pass custom functions - such as sum_squared - through do? is require to map over it?
Thanks again.
Regards

here is a custom funtion


sumsqrd<- function(x){sum(x^2)}

df <- bind_cols(df, {
  rowwise(df) %>% do(z3 = .$x + sumsqrd(.$y))
})
1 Like

There is also a fully purrrless and listless processing approach

#standard tidyverse stuff (no explicit purrr_)
df_new <- df %>% unnest(cols=y) %>% mutate(z=x+y^2) %>%
  group_by(x) %>% summarise(y=list(y),z=list(z))

This might be easier to follow and debug , particularly if at each step a view of the table could be observed, as any one manipulation is very straightforward.

1 Like

Hi @nirgrahamuk,

this code duplicates column z.
What is a purpose of it ?

df <- df %>%
  mutate(z = map2(x, y, ~ map2_dbl(.x, .y, ~ (.x + .y^2))))

df <- bind_cols(df, {
  rowwise(df) %>% do(z2 = .$x + .$y^2)
})

obraz

To show alternative coding styles

Hi,
Is it a way to add something to both of these alternative coding styles above:

in order to have column z2 which will be a sum of c(2,5,10) and c(18,27,38) ?

library(tidyverse)
df = tibble(x = c(1,2),
            y = list(1:3,4:6))
#lists
result1 <- df %>% mutate(z = map2(x, y, ~ map2_dbl(.x, .y, ~ (.x + .y^2))))
result2 <- bind_cols(df, { rowwise(df) %>% do(z2 = .$x + .$y^2) })
#single val sums
result3 <- df %>% rowwise() %>%mutate(w =  sum(map_dbl(y, ~(x+.^2))))
result4 <- bind_cols(df, { rowwise(df) %>% do(w = sum(.$x + .$y^2)) } %>% unnest(cols=c(w)))

Thank you @nirgrahamuk,
that was helpful.

According to this:
https://www.tidyverse.org/blog/2020/04/dplyr-1-0-0-rowwise/
life has become simpler again:

library(tidyverse)
#> Warning: package 'tidyr' was built under R version 4.0.0

df = tibble(x = c(1,2),
            y = list(1:3,4:6))

rwisely <- rowwise(df)

row_wisely <- rwisely %>% mutate(my_sum = sum(x+y^2))

row_wisely
#> # A tibble: 2 x 3
#> # Rowwise: 
#>       x y         my_sum
#>   <dbl> <list>     <dbl>
#> 1     1 <int [3]>     17
#> 2     2 <int [3]>     83

Created on 2020-04-11 by the reprex package (v0.3.0)

obraz

Thanks for your support, it looks very promising; however, in my case, I required a list as an answer - as my question indicated -. I insisted with this because in my project each list is a cash flow with differents discounting periods. The objective is to obtain, following certain steps, the NPV for each CashFlow.
Again, thanks you so much for your help
Regards

Thank you, I apologize for a bit aside (a bit off topic) question here, as I was interested how to obtain a dataframe instead of a list as a result. Always have got doubts if I should start a new topic or whether is it allowed to ask somehow related questions even after the solution has been provided by another user.
best,
Andrzej

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.