当日期无效时,如果不自己修复日期,如何获取年份和月份?

发布于 2024-12-08 17:33:00 字数 1013 浏览 0 评论 0原文

我有一些数据看起来有点像这样:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

宛菡 2024-12-15 17:33:00

假设月份始终在年份之后,并且日期中始终是两个字符。为什么不直接使用 substr 提取信息。也许是这样的:

lapply(X[,'date'], 
  function(x) paste(month.abb[as.numeric(substr(x, 5, 6))], substr(x, 1, 4))
  )

Assuming the month always follows the year and is always two characters in your date. Why not just extract the information with substr. Perhaps something like:

lapply(X[,'date'], 
  function(x) paste(month.abb[as.numeric(substr(x, 5, 6))], substr(x, 1, 4))
  )
灯下孤影 2024-12-15 17:33:00

如果不需要,则无需在格式中指定日期。仔细阅读?strptime。详细信息部分的第二段说:

每个输入字符串
根据指定的格式进行必要的处理:任何
尾随字符将被忽略。

因此,调整你的格式,一切都会正常。

X <- rbind(c(date='20111001', fmt='%Y%m'),
           c('20111031', '%Y%m'),
           c('201110',   '%Y%m'),
           c('102011',   '%m%Y'),
           c('20111000', '%Y%m'))
mapply(as.yearmon, X[, 'date'], X[, 'fmt'], SIMPLIFY=FALSE)

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:

Each input string
is processed as far as necessary for the format specified: any
trailing characters are ignored.

So adjust your format and everything should work.

X <- rbind(c(date='20111001', fmt='%Y%m'),
           c('20111031', '%Y%m'),
           c('201110',   '%Y%m'),
           c('102011',   '%m%Y'),
           c('20111000', '%Y%m'))
mapply(as.yearmon, X[, 'date'], X[, 'fmt'], SIMPLIFY=FALSE)
狼性发作 2024-12-15 17:33:00

假设我总是给出一个日期(而不是时间),并且任何非法“天”都小于 61,我可以通过将提供的日期视为“秒”并替换提供的来保证合法日期,如下所示与第一天。

require(stringr)

safe_date <- str_c('01', X[, 'date'])
safe_fmt <- str_c('%d', str_replace(X[, 'fmt'], '%d', '%S'))

mapply(as.yearmon, safe_date, safe_fmt, SIMPLIFY=FALSE)

# 

假设我总是给出一个日期(而不是时间),并且任何非法“天”都小于 61,我可以通过将提供的日期视为“秒”并替换提供的来保证合法日期,如下所示与第一天。

0120111001` # [1] "Oct 2011" #

假设我总是给出一个日期(而不是时间),并且任何非法“天”都小于 61,我可以通过将提供的日期视为“秒”并替换提供的来保证合法日期,如下所示与第一天。

0120111031` # [1] "Oct 2011" #

假设我总是给出一个日期(而不是时间),并且任何非法“天”都小于 61,我可以通过将提供的日期视为“秒”并替换提供的来保证合法日期,如下所示与第一天。

01201110` # [1] "Oct 2011" #

假设我总是给出一个日期(而不是时间),并且任何非法“天”都小于 61,我可以通过将提供的日期视为“秒”并替换提供的来保证合法日期,如下所示与第一天。

01102011` # [1] "Oct 2011" #

假设我总是给出一个日期(而不是时间),并且任何非法“天”都小于 61,我可以通过将提供的日期视为“秒”并替换提供的来保证合法日期,如下所示与第一天。

0131/10/2011` # [1] "Oct 2011" #

假设我总是给出一个日期(而不是时间),并且任何非法“天”都小于 61,我可以通过将提供的日期视为“秒”并替换提供的来保证合法日期,如下所示与第一天。

0120111000` # [1] "Oct 2011"

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.

require(stringr)

safe_date <- str_c('01', X[, 'date'])
safe_fmt <- str_c('%d', str_replace(X[, 'fmt'], '%d', '%S'))

mapply(as.yearmon, safe_date, safe_fmt, SIMPLIFY=FALSE)

# 

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.

0120111001` # [1] "Oct 2011" #

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.

0120111031` # [1] "Oct 2011" #

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.

01201110` # [1] "Oct 2011" #

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.

01102011` # [1] "Oct 2011" #

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.

0131/10/2011` # [1] "Oct 2011" #

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.

0120111000` # [1] "Oct 2011"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文