Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:写入循环以将NULL替换为日期_R_Loops - Fatal编程技术网

R:写入循环以将NULL替换为日期

R:写入循环以将NULL替换为日期,r,loops,R,Loops,以下是我的表格示例: custID | StartDate | EndDate | ReasonForEnd | TransactionType | TransactionDate 1a | NULL | 2/12/2014 | AccountClosed | AccountOpened | 1/15/2004 1a | NULL | 2/12/2014 | AccountClosed | Purchase | 3/16/2004

以下是我的表格示例:

custID | StartDate | EndDate   | ReasonForEnd  | TransactionType | TransactionDate
    1a |  NULL     | 2/12/2014 | AccountClosed |  AccountOpened  |  1/15/2004
    1a |  NULL     | 2/12/2014 | AccountClosed |  Purchase       |  3/16/2004
    .......
    2b | 7/7/2011  | 6/14/2013 | AccountClosed | AccountOpened   |  8/1/2010
问题与
StartDate
字段有关。对于每个
custId
,如果条目为空,则我希望替换为
TransactionDate
,其中
TransactionType=AccountOpened
。如果
StartDate
TransactionDate
之后,其中
TransactionType=AccountOpened
,则替换为该日期


实际数据超过250000行。我真的需要一些帮助来弄清楚如何在R中编写它。

您可以尝试以下内容,但我还没有测试它。我假设您的data.frame被称为
df

require(dplyr)

df %>%
    mutate_each(funs(as.Date(as.character(., format="%m/%d/%Y"))), 
                StartDate, EndDate, TransactionDate) %>%
    group_by(custID) %>%
    mutate(StartDate = ifelse(is.na(StartDate) | StartDate > TransactionDate[TransactionType == "AccountOpened"], 
                          TransactionDate[TransactionType == "AccountOpened"], StartDate))

此代码首先将多个列转换为
Date
格式(在此步骤中,空条目将转换为NA),按
custID
分组,然后检查
StartDate
是否为
NA
或大于
TransactionDate
其中
TransactionType==“AccountOpened”
,如果为TRUE,将
StartDate
替换为
TransactionDate
其中
TransactionType==“AccountOpened”

不知何故,我认为您是在试图用SQLish的方式实现这一点。。。如果是这样,你应该检查包裹