代码之家  ›  专栏  ›  技术社区  ›  mumber1

转换月份中的日期

  •  0
  • mumber1  · 技术社区  · 1 年前

    我想在一个月内转几天 我试过这个代码

    Df1$days=interval(clinical$date1,clinical$date2, ) %/% months(1)
    

    但20天的结果是1,而不是0.657,我不想四舍五入

    2 回复  |  直到 1 年前
        1
  •  0
  •   Nadir Belhaj    1 年前
    # Load required library
    library(lubridate)
    
    # Assuming you have a data frame named 'clinical' with 'date1' and 'date2' columns
    
    # Calculate the difference in months
    months_diff <- as.numeric(interval(clinical$date1, clinical$date2, "months"))
    
    # Calculate the total number of days between the dates
    days_diff <- as.numeric(difftime(clinical$date2, clinical$date1, units = "days"))
    
    # Calculate the fractional part as days
    fractional_days <- days_diff - months_diff * 30
    
    # Store the fractional days in a new column in the data frame
    clinical$days <- fractional_days
    
    # Print the result
    print(clinical)