How to compute the max value of each row but not every column and mutate 2 columns which is the max value and name?

This is the original dataframe just like this. I want to know the max value between 'a' and 'b' ,and mutate max value and variavble name as two columns.

df <- data.frame(lon = c(102,103,104,105),
                 lat = c(31,32,33,34),
                 a = c(4,3,7,6),
                 b = c(5,2,4,9))

The target dataframe is like this.

dftarget <- data.frame(lon = c(102,103,104,105),
                       lat = c(31,32,33,34),
                       a = c(4,3,7,6),
                       b = c(5,2,4,9),
                       max = c(5,3,7,9),
                       type = c('b','a','a','b'))
1 Like

Can these four alternatives suite your goal?

df <- data.frame(lon = c(102,103,104,105),
                 lat = c(31,32,33,34),
                 a = c(4,3,7,6),
                 b = c(5,2,4,9))
# option 1: Using base R with column indices
dftarget <- data.frame( df, max = apply( df[, -(1:2)], 1, max ),
                        type = colnames(df[, -(1:2)])[max.col(df[, -(1:2)])] )
dftarget
#>   lon lat a b max type
#> 1 102  31 4 5   5    b
#> 2 103  32 3 2   3    a
#> 3 104  33 7 4   7    a
#> 4 105  34 6 9   9    b
# option 2: Using base R with column names
dftarget <- data.frame( df, max = apply( df[c('a', 'b')], 1, max ),
                        type = colnames(df[c('a', 'b')])[max.col(df[c('a', 'b')])] )
dftarget
#>   lon lat a b max type
#> 1 102  31 4 5   5    b
#> 2 103  32 3 2   3    a
#> 3 104  33 7 4   7    a
#> 4 105  34 6 9   9    b

# option 3: Using dplyr package with column indices
dftarget <- dplyr::mutate( df, max =  apply( df[, -(1:2)], 1, max ),
                           type = colnames(df[, -(1:2)])[max.col(df[, -(1:2)])] )
dftarget
#>   lon lat a b max type
#> 1 102  31 4 5   5    b
#> 2 103  32 3 2   3    a
#> 3 104  33 7 4   7    a
#> 4 105  34 6 9   9    b
# option 4: Using dplyr package with column names
dftarget <- dplyr::mutate( df, max =  apply( df[c('a', 'b')], 1, max ),
                           type = colnames(df[c('a', 'b')])[max.col(df[c('a', 'b')])] )
1 Like
library(dplyr)

df %>% mutate(
  max = pmax.int(a, b),
  type = case_when(
          max == a ~ "a",
          TRUE     ~ "b")
  )
2 Likes

Thanks a lot!!!
I am fresh to R.

1 Like

Thanks so much!!!
I am fresh to R.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.