我的数据框看起来大致如下(这意味着它是为了说明而做出的近似值,而不是您可以通过以下链接下载的数据帧的精确副本,或者来自我粘贴在下面的dput()) :
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文件
找到我正在寻找一个能够接受以下输入的函数:
现在,我希望函数只遍历该数据帧的指定列,并替换不匹配点中指定的字符串列表的所有字符串(以及空单元格)上面的图3中的替换字符串在第4点中。但是,只有在满足以下条件时才应该这样做:
正在考虑的小组需要在同一个月有一个时间戳。
例如,假设我们要替换“March_email”列中第8行的空单元格。我可以看到在“March_created_at”列中的第8行有一个时间戳,所以我可以继续用指定的字符串替换这个空单元格(例如“低”)。但是,请查看“April_email”列中的第8行。此单元格也是空的,“April_created_at”列中第8行的单元格也是空的。在这种情况下,不应该做任何事情(即没有插入字符串)。
我想这样做的原因是某些单元格只是空的,因为没有数据,所以不应插入任何内容。其他单元格为空,因为数据丢失,所以我需要根据上面指定的函数来计算数据。
我如何在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 :(得分:3)
这样的事情怎么样:
myfun <- function(month, DF, matches, replacement) {
email.col <- paste0(month, '_actor_attributes_email')
date.col <- paste0(month, '_created_at')
DF[[email.col]] <- ifelse(DF[[date.col]] != '' & !DF[[email.col]] %in% matches,
DF[[email.col]],
replacement)
return (DF[, c(date.col, email.col)])
}
myfun(dat, 'April', 'high', 'foo')
# April_created_at April_actor_attributes_email
# 1 2012-04-01 04:03:13 foo
# 2 2012-04-01 04:03:13 foo
# 3 2012-04-01 04:03:13 foo
# 4 2012-04-01 07:03:11 high
# 5 2012-04-01 07:03:11 high
# 6 2012-04-01 07:03:11 high
然后,你可以喂它几个月......
out <- lapply(list('March', 'April', 'May'),
myfun, DF=dat, matches='', replacement='foo')
你可以快速将其恢复到data.frame中。与plyr
as.data.frame(unlist(out, recursive=FALSE))
还有很多其他方法和选择,但这应该会给你一个很大的开始。