0

New R user here - I have many .csv files containing time stamps (date_time) in one column and temperature readings in the other. I am trying to write a function that detects the date_time format, and then changes it to a different format. Because of the way the data was collected, the date/time format is different in some of the .csv files. I want the function to change the date_time for all files to the same format.

Date_time format I want: %m/%d/%y %H:%M:%S Date_time format I want changed to above: "%y-%m-%d %H:%M:%S"

> head(file1data)
  x         date_time temperature coupler_d coupler_a host_con stopped EoF
1 1 18-07-10 09:00:00      41.137    Logged                               
2 2 18-07-10 09:15:00      41.322                                         
3 3 18-07-10 09:30:00      41.554                                         
4 4 18-07-10 09:45:00      41.832                                         
5 5 18-07-10 10:00:00      42.156                                         
6 6 18-07-10 10:15:00      42.755                                         
> head(file2data)
  x            date_time temperature coupler_d coupler_a host_con stopped EoF
1 1 07/10/18 01:00:00 PM       8.070    Logged                               
2 2 07/10/18 01:15:00 PM       8.095                                         
3 3 07/10/18 01:30:00 PM       8.120                                         
4 4 07/10/18 01:45:00 PM       8.120                                         
5 5 07/10/18 02:00:00 PM       8.020                                         
6 6 07/10/18 02:15:00 PM       7.795 

file2data is in the correct format. file1data is incorrect.

I have tried using logicals to detect and replace the date format e.g.,

file1data %>% 
if(str_match_all(date_time,"([0-9][0-9]{2})[-.])")){
  format(as.POSIXct(date_time,format="%y-%m-%d %H:%M:%S"),"%m/%d/%y %H:%M:%S")
}else{format(date_time,"%m/%d/%y %H:%M:%S")}

but this has not worked, I get the following errors:

Error in if (.) str_match_all(date_time, "([0-9][0-9]{2})[-.])") else { : 
  argument is not interpretable as logical
In addition: Warning message:
In if (.) str_match_all(date_time, "([0-9][0-9]{2})[-.])") else { :
  the condition has length > 1 and only the first element will be used

Any ideas?

  • `if` is not vectorised, see `ifelse` or `dplyr::if_else` – Calum You Oct 14 '19 at 19:54
  • `str_match_all` returns a character matrix, not a logical. Thus the error about logicals, and the warning about length. You probably want to use `stringr::str_detect` or `grepl`. – David Ranzolin Oct 14 '19 at 21:32
  • I think it's better to read your data directly as POSIXct, (e.g. `fileData <- read.csv('file.csv', colClasses = c('date_time' = 'POSIXct'))`). Take a look on [this post](https://stackoverflow.com/questions/13022299/specify-custom-date-format-for-colclasses-argument-in-read-table-read-csv) to see how to create a function to define a specific date format. – Carlos Eduardo Lagosta Oct 14 '19 at 23:14

0 Answers0