1

I have a data frame as follows

S = c("28/05/2016 07:00", "29/05/2016 07:00", "30/05/2016 07:00")
S1 = c("2016-05-28", "2016-05-29", "2016-05-30") 
df = data.frame(S, S1)

I want to convert the dates to day of the year. Using

df$Day_S <- yday(df$S)
df$Day_S1 <- yday(df$S1)

gives

    S                  S1            Day_S   Day_S1
1   28/05/2016 07:00   2016-05-28    141     149
2   29/05/2016 07:00   2016-05-29    140     150
3   30/05/2016 07:00   2016-05-30    140     151

which works only for the format of the 'S1' dates.

Ive tried

df$S_1 <- format(as.POSIXct(df$S,format='%d/%m/%Y'),format='%d/%m/%Y')
df$Day_S_1 <- yday(df$S_1)

but this still gives the wrong day of the year.

How can i convert the 'S' column to day of the year?

Runner Bean
  • 4,895
  • 12
  • 40
  • 60
  • You just forgot the time, i.e. `yday(as.POSIXct(S, format = '%d/%m/%Y %H:%M'))` should work – Sotos Mar 28 '17 at 08:42

1 Answers1

5

This works for me

S = c("28/05/2016 07:00", "29/05/2016 07:00", "30/05/2016 07:00")
s_1 <- as.Date(S,format='%d/%m/%Y')
Day_S <- lubridate::yday(s_1)
Morgan Ball
  • 760
  • 9
  • 23