- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望你没有认为我是在寻求感情方面的建议。
偶尔,我必须让调查受访者能够指定事件发生的时间。结果是一个非常困惑的字符串,老实说我不知道该怎么处理。超越手工重新编码。
这是一个简短的样本,有数千个:
c("May2/ 12 noon", "9:45 am", "11:00 AM AST", "April 27 / 12:00 AST", "11:40 AM AST", "April 25 2011", "April 12th 2011 / 8:44", "April 12 2011 / 8:36am", "April 12 2011 / 8:30am", "April 12th 2011 / 8:18", "April 12 2011 / 8:12am", "April 11th 2011 / 5:57pm", "April 11th 2011 / 5:49pm", "April 11th 2011 / 5:42pm", "April 11th 2011 / 5:36pm", "April 11th 2011 / 5:27", "April 5 @ 11:26am", "8:50", "April 4th 12:45pm", "April 4th around 10am", "April 4th around 10am", "Mar 18, 2011 9:33am", "Mar 18, 2011 9:27am", "df", "fg", "12:16", "9:50", "Feb 8, 2011 / 12:20pm", "8:34 am 2/4/11", "Jan 31, 2011 2:50pm", "Jan 31, 2011 2:45pm", "Jan 31, 2011 2:38pm", "Jan 31, 2011 2:26pm", "11h09", "11:00 am", "1h02 pm", "10h03", "2h10", "Jan 13, 2011 9:50am Van", "Jan 12, 2011", "Jan 12, 2011 3:59pm", "Jan 12 14:19PM", "Jan 12, 2011 1:35pm", "Jan 12,2011 1:28pm", "1h36", "9h15", "9h09", "8h51", "8h45", "8h35", "1h12 pm", "12h59", "11h52 am", "10h45", "15h55", "Dec 31, 10 11:11am", "Dec 31,10 10:15am", "Dec 30, 2010 12:32pm", "Dec 30, 2010 12:18pm", "9:16 am", "11h16 am", "11h12", "9h29 am", "11h38", "Dec 16, 2010", "December 16, 2010", "December 16, 2010", "Dec 15,2010", "DEC 14 2010", "Dec 14 11:38", "Dec 14 11:35", "Dec 14 11:25", "December 13, 2010", "Dec 10, 1:38 pm", "Dec 10, 1:26 pm", "Dec 10, 1:20 pm", "Dec 10, 1:12 pm", "December 9 2010", "11h10 am", "10h59 am", "10:50 am", "Tues Dec 7th, 9:45 Van time", "Dec 3, 2010 12:30pm", "Dec 3, 2010 12:20pm", "Dec 3, 2010 12:10 pm", "November 30, 2010 4.02pm", "November 30, 2010", "november 29 120pm", "November 29 2010 11:27", "10:12am November 29, 2010", "Nov 26/10 1:18pm", "10:56 am", "Nov 24", "nov 24/ 4:20 PM AST", "Nov 24/4:00 PM AST", "NOVEMBER 24/10 2:10 pm", "November 24/10 11:00 a.m.", "12:05 MST", "3.55PM", "Nov. 17/10 12:45 pm", "Nov. 16/10 12:00 noon", "Nov. 16/10 11;50 a.m.", "nov 16/10 11:30 a.m.", "November 12, 2010 @ 12:23pm", "november 11 2010 2:20pm", "November 11 2010 2:15pm", "November 11 2:00pm", "Nov. 10/10:22am", "nov. 8/10...3:19 pm", "Nov 8/10 1;50 p.m.", "November 8/10...12 noon", "November 8/10..10: am", "Nov 5, 2010 1:10 pm", "11:32 am CST", "Nov 4 11:10", "nov 3 10am", "9:30 am", "11/02/2010 1:50PM", "Oct 29/10 2:50PM", "Oct 28 @ 11:20am", "27Oct10 10:40am", "10/26/2010 11:18", "Oct 26/10 11am", "Oct 26/10 10:30 am", "Oct 26 10:50", "10/25/2010 13:50", "10/22/2010 10:15", "Oct 22/10 10AM", "Oct 21, 2010 3:00 pm", "Oct 21, 2010 2:59", "10/21/2010 11:50", "10/21/2010 11:45", "10/21/2010 11:40", "10/21/2010 11:30", "11:30", "Oct 20 approx 1pm", "Oct 20/10 4:50PM", "13:48", "13:45", "Oct 20, 2010 11:45 am", "October 19th 3:05pm", "Oct 18,2010 2:15pm", "Oct 18/10 3:10PM", "10:30 am", "Oct 15/10 11:50am", "oct 14 @ 11:05am", "Oct 14/ 11:06", "4:40 oct 13 atlantic", "oct 13 4:05 pm atlantic", "oct 13 1:45 atlantic time", "Oct 13 / 10:37", "OCT 12 3:33", "Oct 12,2010 1:10pm", "Oct 12 / 11:45", "Oct 12 / 9:45", "Oct 8. 2010/ 2:00", "Oct 8/10- 1145am", "2 Sept 2010 3.52pm", "2 Sept 2010 10.21am", "1 Sept 2010 2.05pm", "1 Sept 2010", "31 Aug 2010 - 11.52am", "31 aug 10:40am", "31 aug 2010 - 10am")
Generally, these events occur near to the date which the respondent fills out the survey, but not always. The survey date is recorded automatically and in a consistent format and is easily to translate into POSIX using as.Date
so, elements that only contain the time can be ignored and merged with the date that they filled out the survey.
Your thoughts are much appreciated.
Note1: Some of you may say, you should have done X, Y, or Z in terms of validating the responses. To you, I say - hell yes - next time. I didn't design it! I just have to deal with it.
A few facts that can assist in a workaround:
What I've done so far:
mos <- strsplit('
jan
feb
mar
apr
may
jun
jul
aug
sep
oct
nov
dec
january
february
march
april
may
june
july
august
september
october
november
december
', '\n')[[1]][-1]
days <- strsplit('
mon
tue
wed
thu
fri
sat
sun
monday
tuesday
wednesday
thursday
friday
saturday
sunday
', '\n')[[1]][-1]
## Messy Date Wrangling
x <- ## that hot ghetto mess above
# minimize
x <- tolower(x)
# remove unnecessary crap
x <- sub("2011"," ",x)
x <- sub("2010"," ",x)
x <- sub("am"," ",x)
x <- sub("pm"," ",x)
x <- sub("[p][.][m]"," ",x)
x <- sub("[a][.][m]"," ",x)
x <- sub("[.]{3}"," ",x)
x <- str_trim(x, side="both")
# divide
x <- strsplit(x,c(" "))
# conquer?
lapply(x, function(x) pmatch(x,mos))
lapply(x, function(x) pmatch(x,days))
最佳答案
我很同情你的约会没有像预期的那么美好。 ;-)
我已经按照@Rguy 建议的思路构建了一个(仍然是部分的)解决方案。
(请注意,这段代码仍然有一个错误:它并不总是返回正确的时间。出于某种原因,它并不总是对冒号之前的数字进行贪婪匹配,因此有时会返回 1:00当时间为 11:00 时。)
首先,构建一个包裹 gsub
和 grep
的辅助函数。该函数采用字符向量作为其参数之一,并将其折叠为由 | 分隔的单个字符串。这样做的效果是允许您轻松传递多个模式以供正则表达式匹配:
find.pattern <- function(x, pattern_list){
pattern <- paste(pattern_list, collapse="|")
ret <- gsub(paste("^.*(", pattern, ").*", sep=""), "\\1", x, ignore.case=TRUE)
ret[ret==x] <- NA
ret2 <- grepl(paste("^(", pattern, ")$", sep=""), x, ignore.case=TRUE)
ret[ret2] <- x[ret2]
ret
}
接下来,使用一些内置变量名称来构造月份和缩写的向量:
all.month <- c(month.name, month.abb)
最后,用不同的提取物构建一个数据框:
ret <- data.frame(
data = dat,
date1 = find.pattern(dat, "\\d+/\\d+/\\d+"),
date2 = find.pattern(dat,
paste(all.month, "\\s*\\d+[(th)|,]*\\s{0,3}[(2010)|(2011)]*", collapse="|", sep="")),
year = find.pattern(dat, c(2010, 2011)),
month = find.pattern(dat, month.abb), #Use base R variable called month.abb for month names
hour = find.pattern(dat, c("\\d+[\\.:h]\\d+", "12 noon")),
ampm = find.pattern(dat, c("am", "pm"))
)
结果:
head(ret, 50)
data date1 date2 year month hour ampm
20 April 4th around 10am <NA> April 4th <NA> Apr <NA> am
21 April 4th around 10am <NA> April 4th <NA> Apr <NA> am
22 Mar 18, 2011 9:33am <NA> Mar 18, 2011 2011 Mar 9:33 am
23 Mar 18, 2011 9:27am <NA> Mar 18, 2011 2011 Mar 9:27 am
24 df <NA> <NA> <NA> <NA> <NA> <NA>
25 fg <NA> <NA> <NA> <NA> <NA> <NA>
26 12:16 <NA> <NA> <NA> <NA> 12:16 <NA>
27 9:50 <NA> <NA> <NA> <NA> 9:50 <NA>
28 Feb 8, 2011 / 12:20pm <NA> Feb 8, 2011 2011 Feb 2:20 pm
29 8:34 am 2/4/11 2/4/11 <NA> <NA> <NA> 8:34 am
30 Jan 31, 2011 2:50pm <NA> Jan 31, 2011 2011 Jan 2:50 pm
31 Jan 31, 2011 2:45pm <NA> Jan 31, 2011 2011 Jan 2:45 pm
32 Jan 31, 2011 2:38pm <NA> Jan 31, 2011 2011 Jan 2:38 pm
33 Jan 31, 2011 2:26pm <NA> Jan 31, 2011 2011 Jan 2:26 pm
34 11h09 <NA> <NA> <NA> <NA> 11h09 <NA>
35 11:00 am <NA> <NA> <NA> <NA> 1:00 am
36 1h02 pm <NA> <NA> <NA> <NA> 1h02 pm
37 10h03 <NA> <NA> <NA> <NA> 10h03 <NA>
38 2h10 <NA> <NA> <NA> <NA> 2h10 <NA>
39 Jan 13, 2011 9:50am Van <NA> Jan 13, 2011 2011 Jan 9:50 am
40 Jan 12, 2011 <NA> Jan 12, 2011 2011 Jan <NA> <NA>
关于r - 处理凌乱的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6194285/
我正在从 Stata 迁移到 R(plm 包),以便进行面板模型计量经济学。在 Stata 中,面板模型(例如随机效应)通常报告组内、组间和整体 R 平方。 I have found plm 随机效应
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 6年前关闭。 Improve this qu
我想要求用户输入整数值列表。用户可以输入单个值或一组多个值,如 1 2 3(spcae 或逗号分隔)然后使用输入的数据进行进一步计算。 我正在使用下面的代码 EXP <- as.integer(rea
当 R 使用分类变量执行回归时,它实际上是虚拟编码。也就是说,省略了一个级别作为基础或引用,并且回归公式包括所有其他级别的虚拟变量。但是,R 选择了哪一个作为引用,以及我如何影响这个选择? 具有四个级
这个问题基本上是我之前问过的问题的延伸:How to only print (adjusted) R-squared of regression model? 我想建立一个线性回归模型来预测具有 15
我在一台安装了多个软件包的 Linux 计算机上安装了 R。现在我正在另一台 Linux 计算机上设置 R。从他们的存储库安装 R 很容易,但我将不得不使用 安装许多包 install.package
我正在阅读 Hadley 的高级 R 编程,当它讨论字符的内存大小时,它说: R has a global string pool. This means that each unique strin
我们可以将 Shiny 代码写在两个单独的文件中,"ui.R"和 "server.R" , 或者我们可以将两个模块写入一个文件 "app.R"并调用函数shinyApp() 这两种方法中的任何一种在性
我正在使用 R 通过 RGP 包进行遗传编程。环境创造了解决问题的功能。我想将这些函数保存在它们自己的 .R 源文件中。我这辈子都想不通怎么办。我尝试过的一种方法是: bf_str = print(b
假设我创建了一个函数“function.r”,在编辑该函数后我必须通过 source('function.r') 重新加载到我的全局环境中。无论如何,每次我进行编辑时,我是否可以避免将其重新加载到我的
例如,test.R 是一个单行文件: $ cat test.R # print('Hello, world!') 我们可以通过Rscript test.R 或R CMD BATCH test.R 来
我知道我可以使用 Rmd 来构建包插图,但想知道是否可以更具体地使用 R Notebooks 来制作包插图。如果是这样,我需要将 R Notebooks 编写为包小插图有什么不同吗?我正在使用最新版本
我正在考虑使用 R 包的共享库进行 R 的站点安装。 多台计算机将访问该库,以便每个人共享相同的设置。 问题是我注意到有时您无法更新包,因为另一个 R 实例正在锁定库。我不能要求每个人都关闭它的 R
我知道如何从命令行启动 R 并执行表达式(例如, R -e 'print("hello")' )或从文件中获取输入(例如, R -f filename.r )。但是,在这两种情况下,R 都会运行文件中
我正在尝试使我当前的项目可重现,因此我正在创建一个主文档(最终是一个 .rmd 文件),用于调用和执行其他几个文档。这样我自己和其他调查员只需要打开和运行一个文件。 当前设置分为三层:主文件、2 个读
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 5年前关闭。 Improve this qu
我的 R 包中有以下描述文件 Package: blah Title: What the Package Does (one line, title case) Version: 0.0.0.9000
有没有办法更有效地编写以下语句?accel 是一个数据框。 accel[[2]]<- accel[[2]]-weighted.mean(accel[[2]]) accel[[3]]<- accel[[
例如,在尝试安装 R 包时 curl作为 usethis 的依赖项: * installing *source* package ‘curl’ ... ** package ‘curl’ succes
我想将一些软件作为一个包共享,但我的一些脚本似乎并不能很自然地作为函数运行。例如,考虑以下代码块,其中“raw.df”是一个包含离散和连续类型变量的数据框。函数“count.unique”和“squa
我是一名优秀的程序员,十分优秀!