0

我有一个大致如下所示的数据框:

March_created_at    March_email March_type  April_created_at April_email    April_type
3/11/12 7:28    jeremy@asynk.ch PushEvent   4/1/12 4:03                     PushEvent
3/11/12 7:28    jeremy@asynk.ch PushEvent   4/1/12 4:03                     PushEvent
3/11/12 7:28    jeremy@asynk.ch PushEvent   4/1/12 4:03                     PushEvent
3/11/12 7:28    jeremy@asynk.ch PushEvent   4/1/12 7:03     high            IssuesEvent
3/11/12 11:06   medium          PushEvent   4/1/12 13:57    medium          PushEvent
3/11/12 11:06   medium          PushEvent   4/1/12 13:57    medium          PushEvent
3/11/12 11:06   medium          PushEvent   4/1/12 13:57    medium          PushEvent
3/11/12 12:46                   PushEvent   
3/11/12 12:46                   PushEvent   
3/11/12 12:46                   PushEvent   

完整的数据集可以在这里以 CSV 文件的形式找到

现在,我正在使用一个函数(感谢 Hadley Wickham)用字符串(例如“high”和“medium”)替换某些电子邮件地址。

# the find-and-replace function
replace_all <- function(df, pattern, replacement) {
  char <- vapply(df, function(x) is.factor(x) || is.character(x), logical(1))
  df[char] <- lapply(df[char], str_replace_all, pattern, replacement)  
  df
}

# the function call
df.new <- replace_all(df, fixed("bford@engineyard.com"), "core")

但是,有些单元格没有写入任何内容(例如“March_email”列中的第 8-10 行)。如果以下条件成立,我想找到所有这些单元格并用字符串“low”替换它们:

*同月附有日期(例如第8-10行的“March_created_at”列中有日期,因此“March_email”中的空单元格表示缺少需要替换的数据)

这意味着如果在电子邮件列中有空白单元格的那一行没有附加日期(例如,4 月的第 8-10 列),则不应在那里替换任何内容。根本没有该范围的数据。

我怎样才能在 R 中做到这一点?

附录:这是数据集头部的 dput():

structure(list(March_created_at = c("2012-03-11 07:28:04", "2012-03-11 07:28:04", 
"2012-03-11 07:28:04", "2012-03-11 07:28:19", "2012-03-11 07:28:19", 
"2012-03-11 07:28:19"), March_actor_attributes_email = c("jeremy@asynk.ch", 
"jeremy@asynk.ch", "jeremy@asynk.ch", "jeremy@asynk.ch", "jeremy@asynk.ch", 
"jeremy@asynk.ch"), March_type = c("PushEvent", "PushEvent", 
"PushEvent", "PushEvent", "PushEvent", "PushEvent"), April_created_at = c("2012-04-01     04:03:13", 
"2012-04-01 04:03:13", "2012-04-01 04:03:13", "2012-04-01 07:03:11", 
"2012-04-01 07:03:11", "2012-04-01 07:03:11"), April_actor_attributes_email = c("", 
"", "", "high", "high", "high"), April_type = c("PushEvent", 
"PushEvent", "PushEvent", "IssuesEvent", "IssuesEvent", "IssuesEvent"
), May_created_at = c("2012-05-01 00:16:05", "2012-05-01 00:16:05", 
"2012-05-01 00:16:05", "2012-05-01 01:03:19", "2012-05-01 01:03:19", 
"2012-05-01 01:03:19"), May_actor_attributes_email = c("john.firebaugh@gmail.com", 
"john.firebaugh@gmail.com", "john.firebaugh@gmail.com", "mitch.tishmack@gmail.com", 
"mitch.tishmack@gmail.com", "mitch.tishmack@gmail.com"), May_type = c("PushEvent", 
"PushEvent", "PushEvent", "IssueCommentEvent", "IssueCommentEvent", 
"IssueCommentEvent"), June_created_at = c("2012-06-01 00:25:05", 
"2012-06-01 00:25:05", "2012-06-01 00:25:05", "2012-06-01 00:42:29", 
"2012-06-01 00:42:29", "2012-06-01 00:42:29"), June_actor_attributes_email =     c("michaelklishin@me.com", 
"michaelklishin@me.com", "michaelklishin@me.com", "", "", ""), 
    June_type = c("IssueCommentEvent", "IssueCommentEvent", "IssueCommentEvent", 
    "PushEvent", "PushEvent", "PushEvent"), July_created_at = c("2012-07-01 13:46:20", 
    "2012-07-01 13:46:20", "2012-07-02 11:53:37", "2012-07-02 11:53:37", 
    "2012-07-02 12:27:30", "2012-07-02 12:27:30"), July_actor_attributes_email = c("medium", 
    "medium", "ryoqun@gmail.com", "ryoqun@gmail.com", "ryoqun@gmail.com", 
    "ryoqun@gmail.com"), July_type = c("PushEvent", "PushEvent", 
    "CreateEvent", "CreateEvent", "PushEvent", "PushEvent"), 
    August_created_at = c("2012-08-01 00:04:09", "2012-08-01 00:04:09", 
    "2012-08-01 00:04:42", "2012-08-01 00:04:42", "2012-08-01 00:05:04", 
    "2012-08-01 00:05:04"), August_actor_attributes_email = c("jeremy@asynk.ch", 
    "jeremy@asynk.ch", "jeremy@asynk.ch", "jeremy@asynk.ch", 
    "jeremy@asynk.ch", "jeremy@asynk.ch"), August_type = c("IssueCommentEvent", 
    "IssueCommentEvent", "IssuesEvent", "IssuesEvent", "IssueCommentEvent", 
    "IssueCommentEvent"), September_created_at = c("2012-09-01 18:12:24", 
    "2012-09-01 18:12:24", "2012-09-01 23:51:18", "2012-09-01 23:51:18", 
    "2012-09-02 00:34:54", "2012-09-02 00:34:54"), September_actor_attributes_email = c("ryoqun@gmail.com", 
    "ryoqun@gmail.com", "ryoqun@gmail.com", "ryoqun@gmail.com", 
    "ryoqun@gmail.com", "ryoqun@gmail.com"), September_type = c("CommitCommentEvent", 
    "CommitCommentEvent", "CreateEvent", "CreateEvent", "PushEvent", 
    "PushEvent"), October_created_at = c("2012-10-01 07:48:38", 
    "2012-10-01 10:01:40", "2012-10-01 10:01:43", "2012-10-01 10:17:00", 
    "2012-10-01 16:08:29", "2012-10-01 18:06:46"), October_actor_attributes_email = c("medium", 
    "medium", "medium", "medium", "", "core"), October_type = c("PushEvent", 
    "IssuesEvent", "PushEvent", "PushEvent", "ForkEvent", "PullRequestEvent"
    )), .Names = c("March_created_at", "March_actor_attributes_email", 
"March_type", "April_created_at", "April_actor_attributes_email", 
"April_type", "May_created_at", "May_actor_attributes_email", 
"May_type", "June_created_at", "June_actor_attributes_email", 
"June_type", "July_created_at", "July_actor_attributes_email", 
"July_type", "August_created_at", "August_actor_attributes_email", 
"August_type", "September_created_at", "September_actor_attributes_email", 
"September_type", "October_created_at", "October_actor_attributes_email", 
"October_type"), row.names = c(NA, 6L), class = "data.frame") 
4

1 回答 1

0

我认为原则上任何一个都应该起作用。

1)
df2[df2$March_actor_attributes_email == "" & df2$March_created_at !="","March_actor_attributes_email"] <- "low"

2)
df2$March_actor_attributes_email <- ifelse(df2$March_actor_attributes_email == "" & df2$March_created_at !="", "low", df2$March_actor_attributes_email)

棘手的部分是日期列。您可能希望确保该字段实际上包含一个日期,而不是仅仅包含非空白,但这取决于您如何构建事物。

于 2012-10-15T20:31:40.093 回答