简体   繁体   English

如何将 m/d/y 和 mdy 中的日期转换为 R 中的单一格式

[英]How to convert dates in m/d/y and m-d-y to a single format in R

activity$ActivityDate = as.POSIXct(activity$ActivityDate, format="%d/%m/%y", tz=Sys.timezone()) returns NA in ActivityDate and Date columns in R activity$ActivityDate = as.POSIXct(activity$ActivityDate, format="%d/%m/%y", tz=Sys.timezone()) 在 R 中的 ActivityDate 和 Date 列中返回 NA

activity$ActivityDate = as.POSIXct(activity$ActivityDate, format="%d/%m/%y", tz=Sys.timezone()) ends up with NA in ActivityDate column in R activity$ActivityDate = as.POSIXct(activity$ActivityDate, format="%d/%m/%y", tz=Sys.timezone()) 在 R 的 ActivityDate 列中以 NA 结尾

I can only guess what you would like to achieve based on your question title - it would help if you could include a minimal reproducible example and some prose describing your problem.我只能根据您的问题标题猜测您想要实现的目标 - 如果您可以包含一个最小的可重现示例和一些描述您的问题的散文,那将会有所帮助。

It seems that lubridate 's parsing functions might do what you need:似乎lubridate的解析函数可以满足您的需要:

ActivityDate <-  c("07/31/2022", "07-31-2022")
lubridate::mdy(ActivityDate)
#> [1] "2022-07-31" "2022-07-31"

Created on 2022-12-21 with reprex v2.0.2创建于 2022-12-21,使用reprex v2.0.2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 R 中将日期从 dmy 重新排列为 mdy? - How can I rearrange the date from d-m-y to m-d-y in R? R 将“Ymd”或“m/d/Y”转换为相同格式 - R convert "Y-m-d" or "m/d/Y" to the same format as.Date 日期格式为 m/d/y in R - as.Date with dates in format m/d/y in R 如何在r数据帧中将日期时间格式“%Y-%m-%d%H:%M:%S”转换为“%Y-%m-%d%H:%M:%S.sss”? 第二部分 - how to convert date time format “%Y-%m-%d %H:%M:%S” to “%Y-%m-%d %H:%M:%S.sss” in r dataframe? second with fraction 将不规则时间序列MDY hh:mm:ss转换为使用NA填充的常规TS - Convert an irregular time series M-D-Y hh:mm:ss to regular TS filling with NA 将格式为“%B-%d-%Y-%I:%M-%p”的字符串转换为R中格式为“%Y%m%d”的日期 - Convert a character string in Format "%B-%d-%Y-%I:%M-%p" into a Date with Format "%Y%m%d" in R 将 r 中的小时日期 %Y/%m/%d 转换为 %Y/%m/%d %H:%M - Convert hourly date %Y/%m/%d to %Y/%m/%d %H:%M in r R 中有没有办法将我的 DateTime 列转换为日期和时间格式(“%m/%d/%Y”和“%h/%m/%s”) - Is there a way in R to convert my DateTime column into Date and Time with the format ("%m/%d/%Y" and "%h/%m/%s") 如何将年份转换为%Y%m%d%H:%M:%S? - How to convert year into %Y%m%d %H:%M:%S? 将日期从以下格式解析为:“%B%d,%Y”为格式:R中的“%Y-%m-%d” - Parse a date from a format like: “%B %d, %Y” to a format: “%Y-%m-%d” in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM