当日期无效时,如果不自己修复日期,如何获取年份和月份?
我有一些数据看起来有点像这样:
require(zoo)
X <- rbind(c(date='20111001', fmt='%Y%m%d'),
c('20111031', '%Y%m%d'),
c('201110', '%Y%m'),
c('102011', '%m%Y'),
c('31/10/2011', '%d/%m/%Y'),
c('20111000', '%Y%m%d'))
print(X)
# date fmt
# [1,] "20111001" "%Y%m%d"
# [2,] "20111031" "%Y%m%d"
# [3,] "201110" "%Y%m"
# [4,] "102011" "%m%Y"
# [5,] "31/10/2011" "%d/%m/%Y"
# [6,] "20111000" "%Y%m%d"
我只想要年份和月份。我不需要这一天,所以我不担心最后一天无效。不幸的是,R 是:
mapply(as.yearmon, X[, 'date'], X[, 'fmt'], SIMPLIFY=FALSE)
# $`20111001`
# [1] "Oct 2011"
# $`20111031`
# [1] "Oct 2011"
# $`201110`
# [1] "Oct 2011"
# $`102011`
# [1] "Oct 2011"
# $`31/10/2011`
# [1] "Oct 2011"
# $`20111000`
# Error in charToDate(x) :
# character string is not in a standard unambiguous format
我知道通常的答案是修复日期的日期部分,例如使用 paste(x, '01', sep='')
。我认为这在这里不起作用,因为我事先不知道日期格式是什么,因此如果不先转换为某种日期对象,我就无法设置日期。
I have some data that looks a bit like this:
require(zoo)
X <- rbind(c(date='20111001', fmt='%Y%m%d'),
c('20111031', '%Y%m%d'),
c('201110', '%Y%m'),
c('102011', '%m%Y'),
c('31/10/2011', '%d/%m/%Y'),
c('20111000', '%Y%m%d'))
print(X)
# date fmt
# [1,] "20111001" "%Y%m%d"
# [2,] "20111031" "%Y%m%d"
# [3,] "201110" "%Y%m"
# [4,] "102011" "%m%Y"
# [5,] "31/10/2011" "%d/%m/%Y"
# [6,] "20111000" "%Y%m%d"
I only want the year and month. I don't need the day, so I'm not worried that the final day is invalid. R, unfortunately, is:
mapply(as.yearmon, X[, 'date'], X[, 'fmt'], SIMPLIFY=FALSE)
# I have some data that looks a bit like this:
require(zoo)
X <- rbind(c(date='20111001', fmt='%Y%m%d'),
c('20111031', '%Y%m%d'),
c('201110', '%Y%m'),
c('102011', '%m%Y'),
c('31/10/2011', '%d/%m/%Y'),
c('20111000', '%Y%m%d'))
print(X)
# date fmt
# [1,] "20111001" "%Y%m%d"
# [2,] "20111031" "%Y%m%d"
# [3,] "201110" "%Y%m"
# [4,] "102011" "%m%Y"
# [5,] "31/10/2011" "%d/%m/%Y"
# [6,] "20111000" "%Y%m%d"
I only want the year and month. I don't need the day, so I'm not worried that the final day is invalid. R, unfortunately, is:
20111001`
# [1] "Oct 2011"
# I have some data that looks a bit like this:
require(zoo)
X <- rbind(c(date='20111001', fmt='%Y%m%d'),
c('20111031', '%Y%m%d'),
c('201110', '%Y%m'),
c('102011', '%m%Y'),
c('31/10/2011', '%d/%m/%Y'),
c('20111000', '%Y%m%d'))
print(X)
# date fmt
# [1,] "20111001" "%Y%m%d"
# [2,] "20111031" "%Y%m%d"
# [3,] "201110" "%Y%m"
# [4,] "102011" "%m%Y"
# [5,] "31/10/2011" "%d/%m/%Y"
# [6,] "20111000" "%Y%m%d"
I only want the year and month. I don't need the day, so I'm not worried that the final day is invalid. R, unfortunately, is:
20111031`
# [1] "Oct 2011"
# I have some data that looks a bit like this:
require(zoo)
X <- rbind(c(date='20111001', fmt='%Y%m%d'),
c('20111031', '%Y%m%d'),
c('201110', '%Y%m'),
c('102011', '%m%Y'),
c('31/10/2011', '%d/%m/%Y'),
c('20111000', '%Y%m%d'))
print(X)
# date fmt
# [1,] "20111001" "%Y%m%d"
# [2,] "20111031" "%Y%m%d"
# [3,] "201110" "%Y%m"
# [4,] "102011" "%m%Y"
# [5,] "31/10/2011" "%d/%m/%Y"
# [6,] "20111000" "%Y%m%d"
I only want the year and month. I don't need the day, so I'm not worried that the final day is invalid. R, unfortunately, is:
201110`
# [1] "Oct 2011"
# I have some data that looks a bit like this:
require(zoo)
X <- rbind(c(date='20111001', fmt='%Y%m%d'),
c('20111031', '%Y%m%d'),
c('201110', '%Y%m'),
c('102011', '%m%Y'),
c('31/10/2011', '%d/%m/%Y'),
c('20111000', '%Y%m%d'))
print(X)
# date fmt
# [1,] "20111001" "%Y%m%d"
# [2,] "20111031" "%Y%m%d"
# [3,] "201110" "%Y%m"
# [4,] "102011" "%m%Y"
# [5,] "31/10/2011" "%d/%m/%Y"
# [6,] "20111000" "%Y%m%d"
I only want the year and month. I don't need the day, so I'm not worried that the final day is invalid. R, unfortunately, is:
102011`
# [1] "Oct 2011"
# I have some data that looks a bit like this:
require(zoo)
X <- rbind(c(date='20111001', fmt='%Y%m%d'),
c('20111031', '%Y%m%d'),
c('201110', '%Y%m'),
c('102011', '%m%Y'),
c('31/10/2011', '%d/%m/%Y'),
c('20111000', '%Y%m%d'))
print(X)
# date fmt
# [1,] "20111001" "%Y%m%d"
# [2,] "20111031" "%Y%m%d"
# [3,] "201110" "%Y%m"
# [4,] "102011" "%m%Y"
# [5,] "31/10/2011" "%d/%m/%Y"
# [6,] "20111000" "%Y%m%d"
I only want the year and month. I don't need the day, so I'm not worried that the final day is invalid. R, unfortunately, is:
31/10/2011`
# [1] "Oct 2011"
# I have some data that looks a bit like this:
require(zoo)
X <- rbind(c(date='20111001', fmt='%Y%m%d'),
c('20111031', '%Y%m%d'),
c('201110', '%Y%m'),
c('102011', '%m%Y'),
c('31/10/2011', '%d/%m/%Y'),
c('20111000', '%Y%m%d'))
print(X)
# date fmt
# [1,] "20111001" "%Y%m%d"
# [2,] "20111031" "%Y%m%d"
# [3,] "201110" "%Y%m"
# [4,] "102011" "%m%Y"
# [5,] "31/10/2011" "%d/%m/%Y"
# [6,] "20111000" "%Y%m%d"
I only want the year and month. I don't need the day, so I'm not worried that the final day is invalid. R, unfortunately, is:
20111000`
# Error in charToDate(x) :
# character string is not in a standard unambiguous format
I know that the usual answer is to fix the day part of the date, e.g. using paste(x, '01', sep='')
. I don't think that will work here, because I don't know in advance what the date format will be, and therefore I cannot set the day without converting to some sort of date object first.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设月份始终在年份之后,并且
日期
中始终是两个字符。为什么不直接使用substr
提取信息。也许是这样的:Assuming the month always follows the year and is always two characters in your
date
. Why not just extract the information withsubstr
. Perhaps something like:如果不需要,则无需在格式中指定日期。仔细阅读
?strptime
。详细信息部分的第二段说:因此,调整你的格式,一切都会正常。
You don't need to specify the day in your format if you don't need it. Read
?strptime
carefully. The second paragraph in the Details section says:So adjust your format and everything should work.
假设我总是给出一个日期(而不是时间),并且任何非法“天”都小于 61,我可以通过将提供的日期视为“秒”并替换提供的来保证合法日期,如下所示与第一天。
Assuming that I'm always given a date (and never a time), and that any illegal 'day' is less than 61, I can guarantee a legal date as follows, by treating the supplied day as 'seconds' and replacing the supplied day with the 1st.