【问题标题】:R function to adjust column type to date formatR函数将列类型调整为日期格式
【发布时间】:2021-01-25 16:21:09
【问题描述】:

我有一个从 csv 转换而来的大小合适的数据集。包含日期列的文件。我需要将此列从字符类型转换为日期格式。我曾尝试使用parse_date 函数,但它一直返回错误:

 library(tidyverse)

 file1 <- read_csv(file1, na = "-", skip = 7)

 file1$Date <- parse_date(file1$Date, format ="%Y-%B-%d")

Warning: 31 parsing failures.

或者..

Error in parse_vector(x, col_date(format), na = na, locale = locale, trim_ws = trim_ws) : 
 is.character(x) is not TRUE

我该如何解决这个问题?任何指导将不胜感激。

编辑::

 Date       `Minimum temper~ `Maximum temper~ `Rainfall (mm)` 
 <date>                <dbl>            <dbl>           <dbl>            
       
 1 NA                      7.6             15.4             0   NA                            
 2 NA                     -3.8             14.3             0   NA                              
 3 NA                     -3.6             19.5             0   NA                            
 4 NA                      3.7             12.8            13.8 NA                             
 5 NA                     -1               15               0   NA                           
 6 NA                      1.2             13.7             0   NA                            
 # ... with 15 more variables: `Direction of maximum wind gust` <chr> etc..

原始文件1

  Date  `Minimum temper~ `Maximum temper~ `Rainfall (mm)` 
   <chr>            <dbl>            <dbl>           <dbl>            
  1 1/08~              7.6             15.4             0                
  2 2/08~             -3.8             14.3             0                
  3 3/08~             -3.6             19.5             0               
  4 4/08~              3.7             12.8            13.8              
  5 5/08~             -1               15               0                 
  6 6/08~              1.2             13.7             0              
      # ... with 15 more variables: 

【问题讨论】:

  • 您好 Ronak,感谢您的建议。现在已经编辑了我的原始问题以包含前几行数据。
  • 以下是日期列[1] "1/08/2018" "2/08/2018" "3/08/2018" "4/08/2018" "5/08/2018" "6/08/2018" "7/08/2018" [8] "8/08/2018" "9/08/2018" "10/08/2018" "11/08/2018" "12/08/2018" "13/08/2018" "14/08/2018" [15] "15/08/2018" "16/08/2018" "17/08/2018" "18/08/2018" "19/08/2018" "20/08/2018" "21/08/2018" [22] "22/08/2018" "23/08/2018" "24/08/2018" "25/08/2018" "26/08/2018" "27/08/2018" "28/08/2018" [29] "29/08/2018" "30/08/2018" "31/08/2018"中的值
  • 感谢您的上述建议,但我收到以下消息:Warning message: All formats failed to parse. No formats found. 所有值均返回 NA:file1$Date &lt;- lubridate::ymd(file1$Date) &gt; file1$Date [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA [31] NA
  • 是的,抱歉,您需要改用 dmy。请参阅下面的答案。

标签: r date type-conversion format


【解决方案1】:

您可以使用正确格式的as.Date:

file1$Date <- as.Date(file1$Date, '%d/%m/%Y')

或者lubridate:

file1$Date <- lubridate::dmy(file1$Date)

【讨论】:

  • @josie 您应该将其作为一个新问题提出,以便每个帖子只关注一个问题。
【解决方案2】:

我们可以从anytime使用anydate

library(anytime)
file1$Date <- anydate(file1$Date)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-30
    • 2023-02-03
    • 1970-01-01
    • 2016-10-13
    • 2018-04-22
    • 2018-03-16
    • 2022-11-04
    相关资源
    最近更新 更多