Say my dataset has two columns
A = c(1,5,NA,NA,NA)
B = c(NA,NA,10,11,13)
how do I create a new column C that takes the max value from A and the min from B to create a new column:
C= c(NA,5,10,NA,NA)
Say my dataset has two columns
A = c(1,5,NA,NA,NA)
B = c(NA,NA,10,11,13)
how do I create a new column C that takes the max value from A and the min from B to create a new column:
C= c(NA,5,10,NA,NA)
Copyright © 2021 Jogjafile Inc.
In this case, if
which.max(A) == which.min(B), thenAwins ... that can be changed by swapping the order withincase_whenor reversing the nesting of the base Rifelses.dplyr
base R
(I don't really like nested
ifelse, but this is uncomplicated ...)Data
(P.S.: if you know all numbers will be effectively integer, then you can shorten this a little by replacing
seq_along(A) == which.max(A)withA == max(A), etc. The reason I don't start with that is that floating-point equality is imperfect in the presence of high-precision numbers, see Why are these numbers not equal? and https://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f.)