-1

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!

CatN
  • 71
  • 7
  • 2
    Please [make this question reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). You need to include example data, as plain text, and only the code relevant to the problem - I don't see any writing to Excel in your example. – neilfws Jun 06 '19 at 03:32
  • 1
    I can't really follow your question, but `as.Date(Treated_LORs$DATE)` will assume a standard format of year/month/day. `as.Date("11/10/2015")` gives the nonsense result of `"0011-10-20"` because of this. Try `as.Date(Treated_LORs$DATE, format="%d/%m/%Y")` – thelatemail Jun 06 '19 at 03:54

1 Answers1

1

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"
Tony Ladson
  • 3,539
  • 1
  • 23
  • 30
  • that's exactly what I needed, thanks Tony. I am a novice coder working with someone else's code and learning as I go. Your answer was very helpful. – CatN Jun 27 '19 at 02:05
  • Cheers @CatN. please consider giving me an upvote if the answer was useful. – Tony Ladson Jun 27 '19 at 23:48