The new dplyr release 1.0.0 makes it easier to work with rows.
across allows to apply a function over whole columns, selected with dplyrverbs, for example sort and everything() :
set.seed(1)
df <- as.data.frame(matrix(sample.int(5, 25, TRUE), 5, 5))
df
V1 V2 V3 V4 V5
1 1 3 5 5 5
2 4 2 5 5 2
3 1 3 2 1 2
4 2 3 2 1 1
5 5 1 1 5 4
df %>% mutate(across(everything(),sort))
V1 V2 V3 V4 V5
1 1 1 1 1 1
2 1 2 2 1 2
3 2 3 2 5 2
4 4 3 5 5 4
5 5 3 5 5 5
Similarly, I would like to apply a function over selected columns in rows, taking advantage of the updated rowwise dplyr functionalities, without transposing the dataframe.
The nearest solution I found uses c_across:
df %>% rowwise %>%
mutate(sortlist = list(sort(c_across(everything())))) %>%
unnest_wider(sortlist)
# A tibble: 5 x 10
V1 V2 V3 V4 V5 ...1 ...2 ...3 ...4 ...5
<int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
1 1 3 5 5 5 1 3 5 5 5
2 4 2 5 5 2 2 2 4 5 5
3 1 3 2 1 2 1 1 2 2 3
4 2 3 2 1 1 1 1 2 2 3
5 5 1 1 5 4 1 1 4 5 5
but is there a dplyr way to get directly to :
V1 V2 V3 V4 V5
1 1 3 5 5 5
2 2 2 4 5 5
3 1 1 2 2 3
4 1 1 2 2 3
5 1 1 4 5 5
as it was the case with columns?