i want to transfer days in month
i tried this code
Df1$days=interval(clinical$date1,clinical$date2, ) %/% months(1)
but for 20 days the result its 1 and not 0.657, and I don’t want to round it
>Solution :
# 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)