-2

I have a data with a column(experiment_time) of DateTime in this format day month year and hour minute and second (dmy_hms). I want to extract the minutes alone from that column because it is what I need (duration between), the experiments were carried out in interval of 5minutes in R programming. I read about dates and time in lubricate but I still don't know how to resolve the issue

rainfall_data <- read.csv("C:/Users/Esther/Document/experiment_data.csv", header = TRUE)

library(tidyverse)

transmute(experiment_data, experiment_time, hour=experiment_time %/% 100, minute=experiment_time %% 100)

            experiment_time hour minute
1   01-Aug-1970 13:05:00   NA     NA
2   01-Aug-1970 13:10:00   NA     NA
3   01-Aug-1970 13:15:00   NA     NA
Ritchie Sacramento
  • 29,890
  • 4
  • 48
  • 56
esther
  • 1
  • Because no one can see the contents of your CSV file, you should at least post a reproducible example. Have a look [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for examples. – sactyr Oct 16 '19 at 05:55

4 Answers4

0

This will probably work:

rainfall_data <- read.csv("C:/Users/Esther/Document/experiment_data.csv", header = TRUE)
rainfall_data$hour = hour(rainfall_data$experiment_time)
rainfall_data$minute = minute(rainfall_data$experiment_time)

If that doesn't work, it probably means that experiment_time is not already in POSIXct format, in which case, you have to convert it first, e.g.,

rainfall_data <- read.csv("C:/Users/Esther/Document/experiment_data.csv", header = TRUE)
rainfall_data$experiment_time = as.POSIXct(rainfall_data$experiment_time)
rainfall_data$hour = hour(rainfall_data$experiment_time)
rainfall_data$minute = minute(rainfall_data$experiment_time)
webb
  • 4,180
  • 1
  • 17
  • 26
0

I would first load the data as POSIXct and then extract the minutes and hours. Given the fact your data is originating from a csv, there is a high chance it is in string format not POSIXct.

The code is:

require(dplyr)

rainfall_data1 = rainfall_data %>% 
  dplyr::mutate(experiment_time = as.POSIXct(strptime(experiment_time,"%d-%b-%Y %H:%M:%S"))) %>% 
  dplyr::mutate(hour = format(experiment_time,"%H"),
                minute = format(experiment_time,"%M"))

Let me know if it works.

0
library(tidyverse)
library(lubridate)
df%>%
    mutate(experiment_time = dmy_hms(experiment_time),
            hour = hour(experiment_time),
            minute = minute(experiment_time))
      experiment_time hour minute
1 1970-08-01 13:05:00   13      5
2 1970-08-01 13:10:00   13     10
3 1970-08-01 13:15:00   13     15
Onyambu
  • 67,392
  • 3
  • 24
  • 53
0

You could use data.table package in the following way:

library(data.table)
setDT(df)
df[, experiment_time := as.POSIXct(experiment_time, format = "%d-%b-%Y %H:%M:%S")]
df[, c("hour", "minute") := .(hour(experiment_time), minute(experiment_time))]

And then convert your data back to dataframe (in case you are not using data.table) as follows:

setDF(df)