Home > Enterprise >  Keep the max value of each row across columns in R dplyr
Keep the max value of each row across columns in R dplyr

Time:12-02

That might seem silly but I was troubled to find something for R dplyr.

I have a large data frame like this one

library(tidyverse)

set.seed(123)
df <- tibble(id=seq(1:5), col1=runif(5,100,1000), col2=runif(5,100,1000),coln=runif(5,200,1000))
df
#> # A tibble: 5 × 4
#>      id  col1  col2  coln
#>   <int> <dbl> <dbl> <dbl>
#> 1     1  359.  141.  965.
#> 2     2  809.  575.  563.
#> 3     3  468.  903.  742.
#> 4     4  895.  596.  658.
#> 5     5  946.  511.  282.

Created on 2022-12-01 with reprex v2.0.2

I want to find the max value in each row across columns and make my data look like this.


#>      id  col1  col2  coln max
#>   <int> <dbl> <dbl> <dbl> 
#> 1     1  359.  141.  965.  965
#> 2     2  809.  575.  563.  809
#> 3     3  468.  903.  742.  903
#> 4     4  895.  596.  658.  895
#> 5     5  946.  511.  282.  946

I envision something like this, but I fail.

df %>% 
  rowwise() %>% 
  mutate(max = max(col1:coln))

any help or suggestion is highly appreciated

CodePudding user response:

You have to use c_across:

library(tidyverse)

set.seed(123)
df <- tibble(id = seq(1:5), col1 = runif(5, 100, 1000), col2 = runif(5, 100, 1000), coln = runif(5, 200, 1000))

df %>%
  rowwise() %>%
  mutate(max = max(c_across(col1:coln)))
#> # A tibble: 5 × 5
#> # Rowwise: 
#>      id  col1  col2  coln   max
#>   <int> <dbl> <dbl> <dbl> <dbl>
#> 1     1  359.  141.  965.  965.
#> 2     2  809.  575.  563.  809.
#> 3     3  468.  903.  742.  903.
#> 4     4  895.  596.  658.  895.
#> 5     5  946.  511.  282.  946.
  • Related