【问题标题】:Reading numeric Date value from CSV file to data.frame in "R"将数字日期值从 CSV 文件读取到“R”中的 data.frame
【发布时间】:2011-02-07 07:38:26
【问题描述】:
> D <- read.csv("sample1.csv", header = FALSE, sep = ",")

> D
        V1     V2     V3     V4
1 20100316 109825 352120 239065
2 20100317 108625 352020 239000
3 20100318 109125 352324 241065

> D[,1]
[1] 20100316 20100317 20100318

在上面的示例中,如何读取D[,1] 中的数据并将其存储为日期值:2010-03-16、2010-03-17、2010-03-18?我有很多这种格式的数据文件。

TIA,

【问题讨论】:

    标签: r csv formatting dataframe


    【解决方案1】:
    dx = "20100316 20100317 20100318"    
    # if your dates already exists as individual items of mode "character"
    # then obviously, skip this step 
    dx2 = unlist(strsplit(dx, split=" "))
    
    fnx = function(x){as.Date(x, format="%Y%m%d")}
    
    dx3 = fnx(dx2)
    dx3
    # returns: [1] "2010-03-16" "2010-03-17" "2010-03-18"
    
    class(dx3)
    # returns: [1] "Date"
    

    【讨论】:

      猜你喜欢
      • 2012-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-13
      • 2012-11-22
      • 2015-10-13
      • 1970-01-01
      • 2015-11-26
      相关资源
      最近更新 更多