Using R in a dataframe I have four columns of time format that I want to calculate the max time of each row. In some cases there are NAs in random cells. I used apply(X,1,max) but it didn't work. I then tried to replace the NAs with X[is.na(x)]<-0 but it was not successful as well. Can anyone help me with those two issues?
Asked
Active
Viewed 27 times
-1
-
1Please don't post data as images. or in .[NORM format](https://xkcd.com/2116/). Take a look at [how to make a reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Martin Gal May 29 '20 at 08:24
-
2Do you want `apply(df, 1, max, na.rm = TRUE)` ? – Ronak Shah May 29 '20 at 08:26
1 Answers
2
How about adding the column directly with tidyverse and pmax?
Example below:
library(tidyverse)
Date1 = c(as.Date('2020-01-01'), as.Date('2020-05-06'), NA, NA, NA)
Date2 = c(as.Date('2020-07-06'), as.Date('2020-03-06'), NA, NA, NA)
Date3 = c(as.Date('2020-08-06'), NA, NA, NA, as.Date('2020-03-06'))
df = data.frame(Date1 = Date1, Date2 = Date2, Date3 = Date3)
df %>%
mutate(maxdate = pmax(Date1, Date2, Date3, na.rm = TRUE))
Date1 Date2 Date3 maxdate
2020-01-01 2020-07-06 2020-08-06 2020-08-06
2020-05-06 2020-03-06 <NA> 2020-05-06
<NA> <NA> <NA> <NA>
<NA> <NA> <NA> <NA>
<NA> <NA> 2020-03-06 2020-03-06

user176135
- 21
- 1