在数据框中转换日期列 [英] Converting date column in data frame

查看:238
本文介绍了在数据框中转换日期列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R版本:3.1.0

R version: 3.1.0

以下线程已被读取:
- 格式a数据帧中的日期列
- 将日期列的数据框转换为时间序列

The following threads have been read: - format a Date column in a Data Frame - Convert data frame with date column to timeseries

我只需要从.csv文件导入数据框,我需要索引为指定的日期列按日期列。

I simply need to import a data frame from a .csv file, and I need the index to be a date column as specified by the Date column.

df <- read.csv(sti, header=TRUE)
df$Date <- as.Date(df$Date, format="%Y%m%d")

(我已经尝试过其他几个日期格式)

(I've tried several other dateformats)

这应该是相当简单的,但结果是填充N / As的列,错误日期的列o一个错误消息。

This should be fairly simple, but the result is either a column filled with N/As, a column with wrong dates og an error message.

我的csv文件包含Excel日期表单我已经尝试更改Windows中的语言设置。

My csv file contains Excel date formats, and I've tried changing the language settings in Windows.

我知道这是日期格式的东西,但我不知道什么。即使是一个dput()的头似乎已经离开了。这是一个缩写版本:

I know it's something with the date format, but I do not know what. Even a dput() of the head seems way off. Here's a shorted version:

structure(list(Date = c("30/12/2013", "27/12/2013", "23/12/2013", 
"20/12/2013", "19/12/2013", "18/12/2013"), MAERSKA = c(11180, 
11150, 10900, 10770, 10670, 10500), WDH = c(527, 522, 515.5, 
515.5, 512, 504.5), VWS = c(160.2, 159.8, 157, 156.6, 156.5, 
153), TRYG = c(524.5, 523, 520.5, 519, 504, 484), TOP = c(142.8, 
142.5, 141.9, 141.5, 139.9, 136.5), TDC = c(52.6, 52.7, 52.45, 
51.95, 51.65, 51.85), Pandora = c(294, 301.2, 304, 300.3, 296.1, 
293.1), NOVOB = c(198.8, 197.2, 195.1, 196.3, 195.6, 190.7), 
    Nordea = c(72.05, 71.35, 70.6, 70.1, 68.65, 67.8), COLOB = c(359, 
    358.3, 353.4, 353.2, 350.2, 342), CHR = c(215.4, 215.7, 212.3, 
    209.1, 206.3, 204.2), CARLB = c(600, 596, 586.5, 586, 584, 
    573), MAERSKB = c(11770, 11740, 11510, 11310, 11210, 11070
    ), JYSKE = c(292.5, 288, 284.4, 282.8, 276.9, 275.7), GN = c(133.2, 
    132.3, 130.5, 129, 127.8, 126.2), GENMAB = c(212, 214.9, 
    217.4, 222.5, 221.6, 216.7), FLS = c(296.1, 290.3, 280.3, 
    278.1, 273.6, 267.1), DSV = c(177.8, 178.2, 176.8, 174, 171.2, 
    169.3), DANSKE = c(124.4, 124.3, 124.3, 123.7, 121, 120.3
    ), NOVOZYMESB = c(228.9, 229.9, 228.5, 230.4, 219.9, 215.6
    )), .Names = c("Date", "MAERSKA", "WDH", "VWS", "TRYG", "TOP", 
"TDC", "Pandora", "NOVOB", "Nordea", "COLOB", "CHR", "CARLB", 
"MAERSKB", "JYSKE", "GN", "GENMAB", "FLS", "DSV", "DANSKE", "NOVOZYMESB"
), row.names = c(NA, 6L), class = "data.frame")

附加信息:

> sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United Kingdom.1252 
[2] LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base 

我希望有人有什么可能导致问题的想法。稍后,我需要将数据框转换为动物园对象,因为包性能分析需要输入。

I hope someone has an idea for what might cause the problem. Later on I need to convert the dataframe to a zoo object, since the package PerformanceAnalytics needs this as input.

推荐答案

使用数据你上面发贴,Date列是一个字符。由于您的日期看起来像30/12/2013​​格式应为%d /%m /%Y而不是%Y%m%d。获取格式正确进行转换是非常重要的。因此,您应该可以做到这一点。

Using the data you posted above, the Date column is a character. And since your dates look like "30/12/2013" format should be "%d/%m/%Y" not "%Y%m%d". It is very important to get the format correct to make the conversion. Therefore you should be able to do

df$Date <- as.Date(df$Date, format="%d/%m/%Y")

使用示例数据

> class(df$Date)=="Date"
[1] TRUE
> sum(is.na(df$Date))==0
[1] TRUE



so everything looks good.

这篇关于在数据框中转换日期列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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