I have a very simple tibble and I would like to iterate over its rows to apply a function using pmap
function. I think I may have misinterpreted some points on pmap
function but I mostly have difficulty selecting arguments. So
I would like to know whether I should use rowwise
function in this case with pmap
or not. However I haven't seen a case.
The other problem is the selection of variables to iterate over using list or select
function:
# Here is my tibble
# Imagine I would like to apply a `n_distinct` function with pmap on it every rows
df <- tibble(id = c("01", "02", "03","04","05","06"),
A = c("Jan", "Mar", "Jan","Jan","Jan","Mar"),
B = c("Feb", "Mar", "Jan","Jan","Mar","Mar"),
C = c("Feb", "Mar", "Feb","Jan","Feb","Feb")
)
# It is perfectly achievable with `rowwise` and `mutate` and results in my desired output
df %>%
rowwise() %>%
mutate(overal = n_distinct(c_across(A:C)))
# A tibble: 6 x 5
# Rowwise:
id A B C overal
<chr> <chr> <chr> <chr> <int>
1 01 Jan Feb Feb 2
2 02 Mar Mar Mar 1
3 03 Jan Jan Feb 2
4 04 Jan Jan Jan 1
5 05 Jan Mar Feb 3
6 06 Mar Mar Feb 2
# But with `pmap` it won't.
df %>%
select(-id) %>%
mutate(overal = pmap_dbl(list(A, B, C), n_distinct))
# A tibble: 6 x 4
A B C overal
<chr> <chr> <chr> <dbl>
1 Jan Feb Feb 1
2 Mar Mar Mar 1
3 Jan Jan Feb 1
4 Jan Jan Jan 1
5 Jan Mar Feb 1
6 Mar Mar Feb 1
I just need a little bit of explanation on the application of pmap
for rowwise iteration on tibbles, so I highly appreciate any help in advance, thank you.