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的方式实现这一点。。。如果是这样,你应该检查包裹