[英]issue convert date variable from character to data.frame or data.frame to character
I'm extracting excel file to have multiple values in character including dates.我正在提取 excel 文件以在字符中包含多个值,包括日期。 From excel I take date_1.
从 excel 我拿 date_1。 date_2 i get it from postgresql using dbgetquery which is always data.frame.
date_2 我使用始终为 data.frame 的 dbgetquery 从 postgresql 获取它。
date_1 = "2017-05-22"
class(date_1) # this is "character"
date_2 = "2017-05-20"
class(date_2) # this is "data.frame"
days <- difftime(as.Date(date_1), as.Date(date_2), units = "days")
print(days)
calculate_age = julian(as.Date(date_1), as.Date(date_2))
print(calculate_age)
I'm getting below 2 error.我得到低于 2 的错误。
1. "Error in as.Date.default(date_2) :
do not know how to convert 'date_2' to class “Date”"
2. Error in UseMethod("julian") :
no applicable method for 'julian' applied to an object of class "character"
Anyone please suggest.任何人请建议。 I tried many.
我试过很多。 but no help.
但没有帮助。 Also I'm new to R and Postgresql.
我也是 R 和 Postgresql 的新手。
You have a good foundation, but your as.Date
has not specified what date format you are supplying.你有一个很好的基础,但是你的
as.Date
没有指定你提供的日期格式。 By specifying that the date strings are 'yyyy-mm-dd', days
is calculated using the difftime( )
function.通过指定日期字符串为“yyyy-mm-dd”,
days
数使用difftime( )
function 计算。
date_1 = "2017-05-22"
date_2 = "2017-05-20"
days <- difftime(as.Date(date_1,format="%Y-%m-%d"),
as.Date(date_2,format="%Y-%m-%d"),
units = "days")
print(days)
Time difference of 2 days
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.