我的数据框大致如下:
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
可以将完整数据集here作为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")
答案 0 :(得分: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)
棘手的部分是日期栏。您可能希望确保该字段实际上包含一个日期,而不仅仅是非空白,但这取决于您如何构建结构。