Search code examples
ras.date

R code incorrectly writing date format to excel


im working in R to analyse some data and having trouble getting my output file (excel workbook) to correctly display the date in Aus format (DD/MM/YYYY). the input file is csv with sites and dates.

I've checked the excel dates in the input file are all Australian format with the * (adapts to local time format) and without the *. I've even tried making the excel format in US date/time thinking this should at least give the right output even if it's kind of in the wrong order.

#Coerce variables into useful types
Treated_LORs$DATE <- as.Date(Treated_LORs$DATE)
Treated_LORs[, 18:44] <- sapply(Treated_LORs[, 18:44], function(x)     as.numeric(x))

#Remove empty rows if any
Treated_LORs <- Treated_LORs[apply(Treated_LORs, 1, function(x) any(!is.na(x))),]

#Create a list of split data frames, split by site and sampling year
split_Treated_LORs <- split(Treated_LORs, f=list(Treated_LORs$SITENAME, Treated_LORs$Sampling.Year), drop=TRUE)

#Run the [calculate_Daily_Average_RA_PAF_Categories_Option2] function     from the sourced R script above
for(i in 1:length(split_Treated_LORs)){
  calculate_Daily_Average_RA_PAF_Categories_Option2(split_Treated_LORs[[i]])
}

i expect the output to be in Australian date format (DD/MM/YYYY) but it looks to be swapped to US format, e.g. 11/10/2015 (11 Oct 2015) becomes 0011-10-20 which looks like it is reading the input date as YYYY/MM/DD. Please help!


Solution

  • I'm not sure I understand the question but to format dates you could try the dmy (day month year) function in lubridate, or specify a format to as.Date

    library(lubridate)
    
    lubridate::dmy("11/10/2015")
    
    as.Date("11/10/2015", format = "%d/%m/%Y")
    

    Have a look at ?strptime to get information on other formats.

    You can also output a date in a particular format

    x <- as.Date('2011-10-11')
    format(x, format = '%d/%m/%Y')
    [1] "11/10/2011"