Message-ID: <E18C153EBB81024CB60FCE9B4C34D57C37F5DD8F@pl-emsmb11>
Date: 2014-11-09T19:44:55Z
From: Muhuri, Pradip (SAMHSA/CBHSQ)
Subject: Getting the most recent dates in a new column from dates in four columns using the dplyr package (mutate verb)
In-Reply-To: <552929448.127879.1415546255329.JavaMail.yahoo@jws10684.mail.bf1.yahoo.com>
Hi Arun and Dennis,
This is just an FYI.
You're right - In one row, there are all NA's in the four "date" columns. I have tested below the "TRUEness" of the condition Arun has set.
is.logical(data1[rowSums(is.na(data1[,-1]))!=4,])
[1] FALSE
All these 3 approaches below provide the exact same results.
# Approach 1 (suggested by Arun): The code gives the expected results, but with a warning message.
data1 %>%
rowwise() %>%
mutate(oldflag=as.Date(max(mrjdate,cocdate, inhdate, haldate,
na.rm=TRUE), origin='1970-01-01'))
# Approach 2: This code (suggested by Dan) does not provide now a warning message although it provided such message earlier.
data2x <- within(data1, oidflag <- apply(data1[,-1], 1, max, na.rm=TRUE))
# Approach 2: This code (suggested by Mark) does not provide a warning message
data2 <- data1
data2$oidflag <- as.Date(sapply(seq_along(data2$id), function(row) {
if (all(is.na(unlist(data1[row, -1])))) {
max_d <- NA
} else {
max_d <- max(unlist(data1[row, -1]), na.rm = TRUE)
}
max_d}),
origin = "1970-01-01")
########################## ends here ################
Pradip K. Muhuri, PhD
SAMHSA/CBHSQ
1 Choke Cherry Road, Room 2-1071
Rockville, MD 20857
Tel: 240-276-1070
Fax: 240-276-1260
-----Original Message-----
From: arun [mailto:smartpink111 at yahoo.com]
Sent: Sunday, November 09, 2014 10:18 AM
To: Muhuri, Pradip (SAMHSA/CBHSQ); r-help at r-project.org
Subject: Re: [R] Getting the most recent dates in a new column from dates in four columns using the dplyr package (mutate verb)
Dear Pradip,