I was wondering how to switch between row wise and normal (column wise) in dplyr. For example, the below code works but it produces some red-colored logs like a warning. How could I do the "select" command in a normal style (not rowwise):
df <- tibble(id = 1:6, year1 = 15:10, year2 = 13:8, year3 = 30:35, year4 = 40:45)
df %>% rowwise() %>%
mutate(output = list(isoreg(c_across(year1:year4))$yf)) %>%
tidyr::unnest_wider(output) %>%
select(., -starts_with("year"))
I think by easily switching between the mentioned modes the issue would be solved.
Return a named output which avoids the warnings. One way to do that is by using setNames
.
library(dplyr)
df %>%
rowwise() %>%
mutate(output = list(setNames(isoreg(c_across(year1:year4))$yf,
paste0('col', 1:4)))) %>%
tidyr::unnest_wider(output) %>%
select(-starts_with('year'))
# A tibble: 6 x 5
# id col1 col2 col3 col4
# <int> <dbl> <dbl> <dbl> <dbl>
#1 1 14 14 30 40
#2 2 13 13 31 41
#3 3 12 12 32 42
#4 4 11 11 33 43
#5 5 10 10 34 44
#6 6 9 9 35 45