使用as.Date(x) - as.Date(y)后,数字后面有“天”

时间:2022-05-14 19:42:55

Hopefully this is a simple question:

希望这是一个简单的问题:

I'm creating a column in my data_frame that takes the date fifference of two date columns.

我正在我的data_frame中创建一个列,该列采用两个日期列的日期。

I get the correct result but when I call on this data frame again, in this new column, I get "days" written after the number.

我得到了正确的结果但是当我再次调用这个数据框时,在这个新专栏中,我在数字后写了“天”。

 my code is:

 temp <- temp %>%
 mutate(AGEMOS = as.numeric(as.Date(temp$INTERVIEWDATE,"%m/%d/%Y")-
                 as.Date(temp$BDAY,"%m/%d/%Y")) / 30.475)

and, to re-iterate, AGEMOS now has "days written after each numeric value.

并且,为了重新迭代,AGEMOS现在有“在每个数值之后写入的天数”。

Is there any way to get rid of this or change it to Years?

有没有办法摆脱这个或将其改为年?

Thanks

3 个解决方案

#1


1  

You can use function as.numeric with parameter units:

您可以将函数as.numeric与参数单位一起使用:

as.numeric(as.Date("2017-08-20")-as.Date("2017-09-03"), units = "days")

#2


2  

To understand the behavior, you need to recognize that subtracting two dates does not return a numeric vector. It returns a vector of class difftime

要了解行为,您需要认识到减去两个日期不会返回数字向量。它返回类difftime的向量

x <- as.Date("2017-05-11")
y <- as.Date("2017-01-23")

z <- x - y 

class(x) # Date
class(y) # Date
class(z) # difftime

The difftime object has its own print method that prints the units attribute for clarity. This can generate confusion because subsequent operations don't can change the units, but the units attribute of the difftime object won't change.

difftime对象有自己的print方法,为了清晰起见打印units属性。这可能会产生混淆,因为后续操作不能更改单位,但difftime对象的units属性不会更改。

z / 30
Time difference of 3.6 days

I recommend a slightly safer approach to calculating time differences

我建议采用一种更安全的方法来计算时差

as.numeric(difftime(x, y, units = "days"))

I actually do recommend specifying the units argument; depending on how close the objects are, the auto selection of the units in R may not always be the same (this is more likely to be true with POSIXct variables, which are date/time objects. By specifying the units you want to work with, you can avoid confusion.

我实际上建议指定单位参数;根据对象的接近程度,R中单位的自动选择可能并不总是相同(对于POSIXct变量,这是日期/时间对象更可能是这样。通过指定要使用的单位,你可以避免混淆。

#3


1  

You can also use difftime and specify the units you are interested in. i.e. difftime(temp$BDAY, temp$INTERVIEWDATE, units = "days" as long as they parameters are a date class.

您也可以使用difftime并指定您感兴趣的单位,即difftime(temp $ BDAY,temp $ INTERVIEWDATE,units =“days”,只要它们的参数是日期类。

#1


1  

You can use function as.numeric with parameter units:

您可以将函数as.numeric与参数单位一起使用:

as.numeric(as.Date("2017-08-20")-as.Date("2017-09-03"), units = "days")

#2


2  

To understand the behavior, you need to recognize that subtracting two dates does not return a numeric vector. It returns a vector of class difftime

要了解行为,您需要认识到减去两个日期不会返回数字向量。它返回类difftime的向量

x <- as.Date("2017-05-11")
y <- as.Date("2017-01-23")

z <- x - y 

class(x) # Date
class(y) # Date
class(z) # difftime

The difftime object has its own print method that prints the units attribute for clarity. This can generate confusion because subsequent operations don't can change the units, but the units attribute of the difftime object won't change.

difftime对象有自己的print方法,为了清晰起见打印units属性。这可能会产生混淆,因为后续操作不能更改单位,但difftime对象的units属性不会更改。

z / 30
Time difference of 3.6 days

I recommend a slightly safer approach to calculating time differences

我建议采用一种更安全的方法来计算时差

as.numeric(difftime(x, y, units = "days"))

I actually do recommend specifying the units argument; depending on how close the objects are, the auto selection of the units in R may not always be the same (this is more likely to be true with POSIXct variables, which are date/time objects. By specifying the units you want to work with, you can avoid confusion.

我实际上建议指定单位参数;根据对象的接近程度,R中单位的自动选择可能并不总是相同(对于POSIXct变量,这是日期/时间对象更可能是这样。通过指定要使用的单位,你可以避免混淆。

#3


1  

You can also use difftime and specify the units you are interested in. i.e. difftime(temp$BDAY, temp$INTERVIEWDATE, units = "days" as long as they parameters are a date class.

您也可以使用difftime并指定您感兴趣的单位,即difftime(temp $ BDAY,temp $ INTERVIEWDATE,units =“days”,只要它们的参数是日期类。