r ifelse日期不添加日期 [英] r ifelse date not adding days

查看:152
本文介绍了r ifelse日期不添加日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算一个列日期的条件。可以是:

I need to compute a condition over a column date in R. Atable would be:

PIL_final1<-data.frame( prior_day1_cart=c(4,8),
                       prior_day1_comp=c('2014-06-03','2014-06-07'),
                       dia_lim_23_cart=c('201-07-30','201-07-30') )


PIL_final1$prior_day1_comp<-as.Date(PIL_final1$prior_day1_comp, format='%Y-%m-%d')
PIL_final1$dia_lim_23_cart<-as.Date(PIL_final1$dia_lim_23_cart, format='%Y-%m-%d')

所以我使用ifelse:

So I use ifelse:

PIL_final1$llamar_dia<-ifelse(PIL_final1$prior_day1_cart+6>23, 
                             PIL_final1$dia_lim_23_cart , 
                            PIL_final1$prior_day1_comp+6)

但是得到:

> PIL_final1
  prior_day1_cart prior_day1_comp dia_lim_23_cart llamar_dia
1               4      2014-06-03      0201-07-30      16230
2               8      2014-06-07      0201-07-30      16234

如果我这样做:

> PIL_final1$prior_day1_comp+6

[1] "2014-06-09" "2014-06-13"

我得到正确的结果。

我如何做ifelse并获取日期?谢谢。

How can I do the ifelse and get the date? thanks.

另外,如果我尝试这个,我仍然会得到一个数字(尽管不同):

Also if I try this, I still get a number (although different):

> PIL_final1$llamar_dia<-ifelse(PIL_final1$prior_day1_cart+6>23, 
+                              PIL_final1$dia_lim_23_cart , 
+                              as.Date(PIL_final$prior_day1_comp+6,format="%Y-%m-%d"))
> PIL_final1
  prior_day1_cart prior_day1_comp dia_lim_23_cart llamar_dia
1               4      2014-06-03      0201-07-30      16376
2               8      2014-06-07      0201-07-30      16377

版本:

如果我这样做:

> as.Date(ifelse(PIL_final1$prior_day1_cart+6>23, PIL_final1$dia_lim_23_cart , 
+                PIL_final1$prior_day1_comp+6), format="%Y-%m-%d", origin="1970-01-01")

[1] "2014-06-09" "2014-06-13"

我得到正确的结果,但是如果我用矢量结果替换ifelse,我会收到错误的日期:

I get the right results, but if I replace the ifelse with the vector result, I get the wrong dates:

> PIL_final1$llamar_dia<-ifelse(PIL_final1$prior_day1_cart+6>23, 
+                              PIL_final1$dia_lim_23_cart , 
+                              PIL_final$prior_day1_comp+6)


 > as.Date(PIL_final1$llamar_dia, format="%Y-%m-%d", origin="1970-01-01")
[1] "2014-11-02" "2014-11-03"


推荐答案

?ifelse


结果的模式可能取决于测试的值(见示例),有时它更好地使用

The mode of the result may depend on the value of test (see the examples), Sometimes it is better >to use a construction such as

ifelse(测试,是,否) ~~ (tmp< - 是的) ; tmp [!test]< - no [!test]; tmp)

>

Applying this :

dat$d3 <- 
with(dat,{
 tmp <- d2+6; tmp[!(x+6>23)] <- d1[!(x+6>23)]; tmp
})

dat
  x         d1         d2         d3
1 4 2014-06-03 0201-07-30 2014-06-03
2 8 2014-06-07 0201-07-30 2014-06-07

也许你应该修改它来处理测试中的缺失值。

Maybe you should modify this to handle missing values in test.

注意我更改了变量自从你重新命名ally long to type and a real sources of errors。

Note I changed the variables names since yours are really long to type and a real source of errors.

dat <- data.frame( x=c(4,8),
                 d1=c('2014-06-03','2014-06-07'),
                 d2=c('201-07-30','201-07-30') )

这篇关于r ifelse日期不添加日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆