R中的strptime错误:输入字符串太长 [英] strptime error in R: input string is too long

查看:906
本文介绍了R中的strptime错误:输入字符串太长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法将数据从csv转换为适当的日期类.我正在使用1033年日期的csv.我已将CSV保存为"YYYYMMDD"格式

I cannot seem to convert my data from a csv into a proper date class. I am using a csv of 1033 dates. I have saved the CSV in the format 'YYYYMMDD'

这是我导入csv的代码(似乎可以正常工作):

Here is my code for importing the csv (which seems to work):

bd <- read.csv('birthdaysExample.csv', 
           header = FALSE, 
           sep = ',')

我可以在R Studio中看到数据:

I can see the data in R Studio:

> head(bd)
        V1
1 20141125
2 20140608
3 20140912
4 20140526
5 20140220
6 20140619

但是,当我尝试转换日期时,收到错误消息: "strptime(bd,format =%Y%m%d")错误:输入字符串太长."

However, when I attempt to convert the dates I receive the error: "Error in strptime(bd, format = "%Y%m%d") : input string is too long."

下面是我的代码:

better_bds <- strptime(bd,format='%Y%m%d')

我什至尝试检查并确认我的所有约会确实都包含8个字符:

I even have tried to check and verify that all of my dates do in fact have 8 characters:

> table(nchar(bd$V1) != 8 | nchar(bd$V1) != 8)

FALSE 
1033

因此,如果有人能指出正确的方向,我不确定下一步该怎么走,我将不胜感激!

So I'm not sure where to turn next if anyone could point me in the right direction, I would appreciate it!

推荐答案

问题是bd是一个单列data.frame,而strptime需要一个字符向量.如果您没有将字符向量传递给strptime,它将在您传入的任何内容上调用as.character(x).调用as.character(bd)会导致您可能不希望的结果.

The problem is that bd is a one-column data.frame and strptime expects a character vector. If you don't pass a character vector to strptime, it calls as.character(x) on whatever you pass in. Calling as.character(bd) results in something you probably do not expect.

bd <- structure(list(V1 = c(20141125L, 20140608L, 20140912L, 20140526L,
  20140220L, 20140619L)), .Names = "V1", class = "data.frame",
  row.names = c(NA, -6L))
as.character(bd)
# [1] "c(20141125, 20140608, 20140912, 20140526, 20140220, 20140619)"

在将bd的字符向量列传递给strptime之前,需要对其进行子集设置(如

You need to subset the character vector column of bd before passing it to strptime (as Hugh suggested in his comment).

strptime(bd[,1], format="%Y%m%d")

此外,由于您似乎没有任何实际的时间信息,因此建议您改用Date类.这样可以避免您遇到任何潜在的时区问题.

Also, since you do not appear to have any actual time information, I would suggest you use the Date class instead. That will prevent you from encountering any potential timezone issues.

as.Date(as.character(bd[,1]), format="%Y%m%d")

这篇关于R中的strptime错误:输入字符串太长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆