Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Transform date in month

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading