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)
})
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
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.
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