[R] Comparing 2 dale columns

Mark Sharp msharp at TxBiomed.org
Wed Aug 23 17:28:39 CEST 2017


Patrick,

## Run the following script an notice the different values of the dataframe "data" in each instance.

# I understand you have done something like the following:
data <- data.frame(COL1 = c("6/1/14", "7/1/14"),
                   COL2 = c("5/1/15", "5/1/15"), stringsAsFactors = FALSE)
data$Date_Flag <- ifelse(data$COL2 > data$COL1, 0,1)
data
data$COL2 <- as.Date(as.character(data$COL2, format = "%y/%m/%d"))
data$COL1 <- as.Date(as.character(data$COL1, format = "%y/%m/%d"))
data$Date_Flag <- ifelse(data$COL2 > data$COL1, 0,1)
data

# What you may want instead is the following:
data <- data.frame(COL1 = c("6/1/14", "7/1/14"),
                   COL2 = c("5/1/15", "5/1/15"), stringsAsFactors = FALSE)
data
## strptime() converts the character vector to POSIXct so you do not necessarily
## need the as.Date. However, they are not the same and you may need the Date
## class.
data$COL2 <- as.Date(strptime(data$COL2, format = "%m/%d/%y"))
data$COL1 <- as.Date(strptime(data$COL1, format = "%m/%d/%y"))
data
data$Date_Flag <- ifelse(data$COL2 > data$COL1, 0,1)
data


R. Mark Sharp, Ph.D.
msharp at TxBiomed.org





> On Aug 23, 2017, at 9:53 AM, Patrick Casimir <patrcasi at nova.edu> wrote:
>
> data$Date_Flag <- ifelse(data$COL2 > data$COL1, 0,1)
>
>
> COL1       COL2
> 6/1/14     5/1/15
> 7/1/14     5/1/15
>
>
> data$COL2<- as.Date(as.character(data$COL2, format="%y/%m/%d"))
> data$COL1<- as.Date(as.character(data$COL1, format="%y/%m/%d"))
>

CONFIDENTIALITY NOTICE: This e-mail and any files and/or...{{dropped:10}}



More information about the R-help mailing list