How to Prevent Ifelse() from Turning Date Objects into Numeric Objects

How to prevent ifelse() from turning Date objects into numeric objects

You may use data.table::fifelse (data.table >= 1.12.3) or dplyr::if_else.



data.table::fifelse

Unlike ifelse, fifelse preserves the type and class of the inputs.

library(data.table)
dates <- fifelse(dates == '2011-01-01', dates - 1, dates)
str(dates)
# Date[1:5], format: "2010-12-31" "2011-01-02" "2011-01-03" "2011-01-04" "2011-01-05"


dplyr::if_else

From dplyr 0.5.0 release notes:

[if_else] have stricter semantics that ifelse(): the true and false arguments must be the same type. This gives a less surprising return type, and preserves S3 vectors like dates" .

library(dplyr)
dates <- if_else(dates == '2011-01-01', dates - 1, dates)
str(dates)
# Date[1:5], format: "2010-12-31" "2011-01-02" "2011-01-03" "2011-01-04" "2011-01-05"

R ifelse avoiding change in date format

If dat is the dataset. I assume it is is.na(DateOut) from the Travel date column

 as.Date(with(dat, ifelse(is.na(DateOut), DateIn, DateOut)),origin="1970-01-01")
#[1] "2010-11-24" "2012-01-21" "2010-11-25" "2014-01-14"

Or you can do:

 dat$Travel.date <- dat$DateOut
dat$Travel.date[is.na(dat$Travel.date)] <- dat$DateIn[is.na(dat$Travel.date)]
dat
# DateIn DateOut Travel.date
#1 2010-11-24 <NA> 2010-11-24
#2 2011-12-21 2012-01-21 2012-01-21
#3 2010-10-25 2010-11-25 2010-11-25
#4 2014-01-14 <NA> 2014-01-14

ifelse function returns numbers instead of dates

Basically, the new column should be treaty date y, if available, otherwise treaty date x, otherwise nothing. Another option

ResSet2$Treaty_Date=ResSet2$Treaty_Date.y
ResSet2$Treaty_Date[is.na(ResSet2$Treaty_Date)]=
ResSet2$Treaty_Date.x[is.na(ResSet2$Treaty_Date)]

Treaty_Date.x Treaty_Date.y Treaty_Date
1 2020-12-31 <NA> 2020-12-31
2 <NA> 2019-05-22 2019-05-22
3 2020-10-13 2019-09-01 2019-09-01

Preserving column types when applying ifelse() across columns of different types in R

Make use of the default (TRUE) option in case_when which returns the NA based on the type

library(dplyr)
tdf %>%
mutate(across(-valid, ~ case_when(valid ~ .)))

-output

# A tibble: 2 × 4
date team score valid
<date> <fct> <int> <lgl>
1 2021-12-10 T1 3 TRUE
2 NA <NA> NA FALSE

Or another option is replace

tdf %>% 
mutate(across(-valid, ~ replace(., !valid, NA)))
# A tibble: 2 × 4
date team score valid
<date> <fct> <int> <lgl>
1 2021-12-10 T1 3 TRUE
2 NA <NA> NA FALSE

According to ?ifelse

The mode of the result may depend on the value of test (see the examples), and the class attribute (see oldClass) of the result is taken from test and may be inappropriate for the values selected from yes and no.

Sometimes it is better to use a construction such as

(tmp <- yes; tmp[!test] <- no[!test]; tmp)

, possibly extended to handle missing values in test.

New character values based on date range using ifelse

I found the problem vue$Name <- ifelse(vue$Serial == "483689" && vue$Date >= "2019-8-23 10:00:00", "newname", vue$Name)

I was missing the second & and vue$ before Date

Prevent ifelse() in R from turning factor into numeric

There's probably a better way of doing this but keeping your code you can do this:

index <- data1$hr|data1$br
first<-2;last<-length(data1)

for (i in first : last){
if(names(data1)[i]!="att"){
data1[index,i] <- NA }}

ifelse Statement Returning Number Instead Of Date

See the help file for ifelse

Warning:

The mode of the result may depend on the value of ‘test’ (see the
examples), and the class attribute (see ‘oldClass’) of the result
is taken from ‘test’ and may be inappropriate for the values
selected from ‘yes’ and ‘no’.


Sometimes it is better to use a construction such as

  (tmp <- yes; tmp[!test] <- no[!test]; tmp)

, possibly extended to handle missing values in ‘test’.

This describes precisely what is going on in your example -- the date class attribute is lost -- and a work around -- a multi-step approach.

osa$milldate <- osa$date
ind<- osa$dateloca==TRUE & osa$datelocb==TRUE
osa$milldate[!ind] <- osa$dateminus

Another option is replace.



Related Topics



Leave a reply



Submit