How to convert character to POSIXct format in R?

How to convert character to POSIXct format in R?


Here is one dataset

df= data.frame(structure(list(time = c("2022-10-01 00:30:00", "2022-10-01 01:30:00", 
                        "2022-10-01 02:30:00", "2022-10-01 03:30:00", "2022-10-01 04:30:00", 
                        "2022-10-01 05:30:00", "2022-10-01 06:30:00", "2022-10-01 07:30:00", 
                        "2022-10-01 08:30:00", "2022-10-01 09:30:00", "2022-10-01 10:30:00", 
                        "2022-10-01 11:30:00", "2022-10-01 12:30:00", "2022-10-01 13:30:00", 
                        "2022-10-01 14:30:00", "2022-10-01 15:30:00", "2022-10-01 16:30:00", 
                        "2022-10-01 17:30:00", "2022-10-01 18:30:00", "2022-10-01 19:30:00", 
                        "2022-10-01 20:30:00", "2022-10-01 21:30:00", "2022-10-01 22:30:00", 
                        "2022-10-01 23:30:00"), 
                        solar_radiation = c(0, 0, 0, 0, 0, 0, 0, 94.8464713165497, 
                        292.960199004975, 483.373881932022, 636.977557890442, 
                        739.091692307692, 781.551058146702, 760.003039513678, 
                        679.285714285714, 542.340616966581, 365.719847651291, 
                        169.117233294256, 11.3918975358485, 0, 0, 0, 0, 0)), 
                        class = c("tbl_df", "tbl", "data.frame"), 
                        row.names = c(NA, -24L)))

head(df,10)
   time                  solar_radiation
1  2022-10-01 00:30:00         0.00000
2  2022-10-01 01:30:00         0.00000
3  2022-10-01 02:30:00         0.00000
4  2022-10-01 03:30:00         0.00000
5  2022-10-01 04:30:00         0.00000
6  2022-10-01 05:30:00         0.00000
7  2022-10-01 06:30:00         0.00000
8  2022-10-01 07:30:00        94.84647
9  2022-10-01 08:30:00       292.96020
10 2022-10-01 09:30:00       483.37388
.
.
.

Let’s check the data type of each variable.

str(df)

'data.frame':	24 obs. of  2 variables:
$ time: chr "2022-10-01 00:30:00" "2022-10-01 01:30:00" "2022-10-01 02:30:00" "2022-10-01 03:30:00" ...
$ solar_radiation: num  0 0 0 0 0 ...

The time column is in character format. When opening the data in Excel, it is considered text. I wish to create a time series graph, but this cannot be accomplished when the variables are in text format.

Therefore, we need to convert the text to a time format.

df$time=as.POSIXct(df$time)

str(df)
'data.frame': 24 obs. of 2 variables:
$ time: POSIXct, format: "2022-10-01 00:30:00" "2022-10-01 01:30:00" "2022-10-01 02:30:00" "2022-10-01 03:30:00" ...
$ solar_radiation: num  0 0 0 0 0 ...

Now we can adjust time using scale_x_datetime()

ggplot(data=df, aes(x=time, y=solar_radiation)) + 
  geom_line () +
  scale_x_datetime(date_breaks="4 hour", date_labels="%H:%M") +
  scale_y_continuous(breaks=seq(0,1500,500), limits = c(0,1500)) +
  labs(x="Hour", y=bquote("PAR (μ mol "~m^-2*~s^-1*")")) +
  theme_classic(base_size=18, base_family="serif")+
  theme(axis.line=element_line(linewidth=0.5, colour="black")) +
windows(width=5.5, height=5)
full summary: https://github.com/agronomy4future/r_code/blob/main/How_to_convert_character_to_POSIXct_format_in_R.ipynb


Leave a Reply

If you include a website address in the comment section, I cannot see your comment as it will be automatically deleted and will not be posted. Please refrain from including website addresses.