ltlf653
ltlf653

Reputation: 29

How can I get row-wise max based on condition of specific column in R dataframe?

I'm trying to get the maximum value BY ROW across several columns (climatic water deficit -- def_59_z_#) depending on how much time has passed (time since fire -- YEAR.DIFF). Here are the conditions:

However, I am unable to extract a row-wise max when I include a condition. There are several existing posts that address row-wise min and max (examples 1 and 2) and sd (example 3) -- but these don't use conditions. I've tried using apply but I haven't been able to find a solution when I have multiple columns involved as well as a conditional requirement.

The following code simply returns 3.5 in the new column def59_z_max15, which is the maximum value that occurs in the dataframe -- except when YEAR.DIFF is 1, in which case def_50_z_1 is directly returned. But for all the other conditions, I want 0.98, 0.67, 0.7, 1.55, 1.28 -- values that reflect the row maximum of the specified columns. Link to sample data here. How can I achieve this?

I appreciate any/all suggestions!

data <- data %>%
mutate(def59_z_max15 = ifelse(YEAR.DIFF == 1,
                            (def59_z_1),
                            ifelse(YEAR.DIFF == 2,
                                   max(def59_z_1, def59_z_2),
                                   ifelse(YEAR.DIFF == 3,
                                          max(def59_z_1, def59_z_2, def59_z_3),
                                          ifelse(YEAR.DIFF == 4,
                                                 max(def59_z_1, def59_z_2, def59_z_3, def59_z_4),
                                                 max(def59_z_1, def59_z_2, def59_z_3, def59_z_4, def59_z_5))))))

Upvotes: 0

Views: 1661

Answers (2)

Dij
Dij

Reputation: 1378

Throw this function in an apply family function

func <- function(x) {
first.val <- x[1]
if (first.val < 5) {
return(max(x[2:(first.val+)])
} else {
return(max(x[2:6]))
}
}

Your desired output should be obtained by:

apply(data, 1, function(x) func(x)) #do it by row by setting arg2 = 1

Upvotes: 1

akrun
akrun

Reputation: 886948

An option would be to get the pmax (rowwise max - vectorized) for each set of conditions separately in a loop (map - if the value of 'YEAR.DIFF' is 1, select only the 'def_59_z_1', for 2, get the max of 'def_59_z_1' and 'def_59_z_2', ..., for 5, max of 'def_59_z_1' to 'def_59_z_5', coalesce the columns together and replace the rest of the NA with the pmax of all the 'def59_z" columns

library(tidyverse)
out <- map_dfc(1:5, ~
         df1 %>% 
           select(seq_len(.x) + 1) %>% 
           transmute(val = na_if((df1[["YEAR.DIFF"]] == .x)*
               pmax(!!! rlang::syms(names(.))), 0))) %>%  
  transmute(def59_z_max15 = coalesce(!!! rlang::syms(names(.)))) %>%
  bind_cols(df1, .)%>%
  mutate(def59_z_max15 = case_when(is.na(def59_z_max15) ~ 
         pmax(!!! rlang::syms(names(.)[2:6])), TRUE ~ def59_z_max15))
head(out, 10)
#   YEAR.DIFF def59_z_1 def59_z_2 def59_z_3 def59_z_4 def59_z_5 def59_z_max15
#1          5      0.25     -2.11      0.98     -0.07      0.31          0.98
#2          9      0.67      0.65     -0.27      0.52      0.26          0.67
#3         10      0.56      0.33      0.03      0.70     -0.09          0.70
#4          2     -0.34      1.55     -1.11     -0.40      0.94          1.55
#5          4      0.98      0.71      0.41      1.28     -0.14          1.28
#6          3      0.71     -0.17      1.70     -0.57      0.43          1.70
#7          4     -1.39     -1.71     -0.89      0.78      1.22          0.78
#8          4     -1.14     -1.46     -0.72      0.74      1.32          0.74
#9          2      0.71      1.39      1.07      0.65      0.29          1.39
#10         1      0.28      0.82     -0.64      0.45      0.64          0.28

data

df1 <- read.csv("https://raw.githubusercontent.com/CaitLittlef/random/master/data.csv")

Upvotes: 1

Related Questions