更改日期格式
Change of Date format
我有一个带有日期和温度的 table,我需要将日期从这种格式 1/1/2021 12:00:00 AM
转换为 1/1/21
。请帮忙。我尝试使用此代码
添加一个包含新日期的新列
SFtemps$Date <- as.Date(strptime(SFtemps$Date, "%m/%d/%y %H:%M"))
但它不起作用。
你可以试试tryFormats
as.Date(x, format, tryFormats = c("%m/%d/%y, %H:%M"),
optional = FALSE)
用十进制数(01-12)表示小时应该是%I
,而不是%H
,%y
没有世纪的年份 (00–99)。
x <- "1/1/2021 12:00:00 AM"
format(strptime(x, "%m/%d/%Y %I:%M:%S %p"), "%m/%d/%y")
[1] "01/01/21"
请注意,re-foramt时间对象后,它将是一个纯字符串,并失去时间对象的所有属性。
你的格式有点不对。这是两个可行的选项(我最喜欢 lubridate):
SFtemps <- list()
SFtemps$Date <- '1/1/2021 12:00:00 AM'
> as.Date(strptime(SFtemps$Date, "%m/%d/%Y %r"))
[1] "2021-01-01"
> as.Date(lubridate::mdy_hms(SFtemps$Date))
[1] "2021-01-01"
这会给你日期。如果您想将其视为 1/1/2021,请使用 format
函数
您的字符串实际上是一个时间戳,使用 lubridate
您可以将其转换为所需的格式,然后利用 stamp
函数以适合您的方式格式化数据。
org_str <- "1/1/2021 12:00:00 AM"
library("lubridate")
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
x <- mdy_hms(x = org_str, tz = "UTC")
sf <- stamp("29/01/1997")
#> Multiple formats matched: "%d/%Om/%Y"(1), "%d/%m/%Y"(1)
#> Using: "%d/%Om/%Y"
sf(x)
#> [1] "01/01/2021"
由 reprex package (v2.0.1)
于 2022-04-22 创建
说明
与普遍看法相反,日期没有这样的格式。只有日期的字符串表示可以是特定格式。考虑以下示例。 运行代码:
x <- Sys.Date()
unclass(x)
将 return 一个整数。这是因为1:
Thus, objects of the class Date are internally represented as numbers. More specifically, dates in R are actually integers.
同样适用于POSIX个对象
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
xn <- now()
class(xn)
#> [1] "POSIXct" "POSIXt"
unclass(xn)
#> [1] 1650644616
#> attr(,"tzone")
#> [1] ""
由 reprex package (v2.0.1)
于 2022-04-22 创建
你总是可以运行:
format.Date(lubridate::now(), "%d-%m")
# [1] "22-04"
但是,通过这样做,您并不是在格式化日期,而是在创建日期/时间戳对象的字符串表示形式。如果您想在常见的日期时间操作中使用您的字符串表示形式,则必须将其转换为时间戳/日期对象。
建议
每当使用日期时间/日期对象时,我认为最好将这些对象保持正确 class 维护相关详细信息(例如时间戳时的时区)并且仅利用格式化函数在分析/可视化表示中使用数据时,如示例所示。
我有一个带有日期和温度的 table,我需要将日期从这种格式 1/1/2021 12:00:00 AM
转换为 1/1/21
。请帮忙。我尝试使用此代码
SFtemps$Date <- as.Date(strptime(SFtemps$Date, "%m/%d/%y %H:%M"))
但它不起作用。
你可以试试tryFormats
as.Date(x, format, tryFormats = c("%m/%d/%y, %H:%M"),
optional = FALSE)
用十进制数(01-12)表示小时应该是%I
,而不是%H
,%y
没有世纪的年份 (00–99)。
x <- "1/1/2021 12:00:00 AM"
format(strptime(x, "%m/%d/%Y %I:%M:%S %p"), "%m/%d/%y")
[1] "01/01/21"
请注意,re-foramt时间对象后,它将是一个纯字符串,并失去时间对象的所有属性。
你的格式有点不对。这是两个可行的选项(我最喜欢 lubridate):
SFtemps <- list()
SFtemps$Date <- '1/1/2021 12:00:00 AM'
> as.Date(strptime(SFtemps$Date, "%m/%d/%Y %r"))
[1] "2021-01-01"
> as.Date(lubridate::mdy_hms(SFtemps$Date))
[1] "2021-01-01"
这会给你日期。如果您想将其视为 1/1/2021,请使用 format
函数
您的字符串实际上是一个时间戳,使用 lubridate
您可以将其转换为所需的格式,然后利用 stamp
函数以适合您的方式格式化数据。
org_str <- "1/1/2021 12:00:00 AM"
library("lubridate")
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
x <- mdy_hms(x = org_str, tz = "UTC")
sf <- stamp("29/01/1997")
#> Multiple formats matched: "%d/%Om/%Y"(1), "%d/%m/%Y"(1)
#> Using: "%d/%Om/%Y"
sf(x)
#> [1] "01/01/2021"
由 reprex package (v2.0.1)
于 2022-04-22 创建说明
与普遍看法相反,日期没有这样的格式。只有日期的字符串表示可以是特定格式。考虑以下示例。 运行代码:
x <- Sys.Date()
unclass(x)
将 return 一个整数。这是因为1:
Thus, objects of the class Date are internally represented as numbers. More specifically, dates in R are actually integers.
同样适用于POSIX个对象
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
xn <- now()
class(xn)
#> [1] "POSIXct" "POSIXt"
unclass(xn)
#> [1] 1650644616
#> attr(,"tzone")
#> [1] ""
由 reprex package (v2.0.1)
于 2022-04-22 创建你总是可以运行:
format.Date(lubridate::now(), "%d-%m")
# [1] "22-04"
但是,通过这样做,您并不是在格式化日期,而是在创建日期/时间戳对象的字符串表示形式。如果您想在常见的日期时间操作中使用您的字符串表示形式,则必须将其转换为时间戳/日期对象。
建议
每当使用日期时间/日期对象时,我认为最好将这些对象保持正确 class 维护相关详细信息(例如时间戳时的时区)并且仅利用格式化函数在分析/可视化表示中使用数据时,如示例所示。