0

I am running some analysis code in R studio and on my Mac I get an error that prevents me continuing, but when I run the same code on a PC I only get a warning, which then allows me to continue with the analysis.

convert.dates <- function(data, columns, format="%Y-%m-%d %H:%M:%S") {
  if (length(columns) == 1) {
    posdates <- data.frame(strptime(data[, columns], format))
    names(posdates) <- columns
  } else
    posdates <- as.data.frame(apply(data[, columns], 2, strptime, format))
  data <- data[, -match(columns, names(data))]
  data.frame(data, posdates)
}
recdat$datetime <- as.character(strptime(recdat$datetime, "%Y:%m:%d %H:%M:%S"))
recdat <- convert.dates(recdat, "datetime")

Mac error code: Error in strptime(data[, columns], format) : input string is too long

PC warning: row names were found from a short variable and were discarded

Why might these be different and how can I ultimately avoid receiving the error?

`structure(list(placeID = c("SHPP9", "SHPP9", "MAPP4", "MAPP4", 
"MAPP4", "MAPP4", "HAPP15", "HAPP15", "HAPP15", "DEPP2"), fileID = c("IMG_0086_grid_compass.JPG", 
"IMG_0089_grid_compass.JPG", "IMG_0146_grid_compass.JPG", "IMG_0149_grid_compass.JPG", 
"IMG_0152_grid_compass.JPG", "IMG_0155_grid_compass.JPG", "IMG_2671_grid_compass.JPG", 
"IMG_2673_grid_compass.JPG", "IMG_2676_grid_compass.JPG", "IMG_0008_grid_compass.JPG"
), datetime = c("2019:06:01 02:59:22", "2019:06:01 02:59:23", 
"2019:06:06 00:45:32", "2019:06:06 00:45:33", "2019:06:06 00:45:49", 
"2019:06:06 00:45:50", "2019:06:07 23:05:46", "2019:06:07 23:05:47", 
"2019:06:07 23:05:48", "2019:06:10 22:29:47"), time = c(0.782634725415124, 
0.78270744746729, 31.6146031824166, 31.6146759044688, 31.6158394573035, 
31.6159121793556, 43.7456595925075, 43.7457323145597, 43.7458050366119, 
62.438208603419), species = c("Civet", "Civet", "Civet", "Civet", 
"Civet", "Civet", "Civet", "Civet", "Civet", "Civet"), absangle = c(0.233516621633944, 
0.22839855680029, 0.0168248958289925, 0.112940296923411, 0.00228507404113898, 
0.0993839863232428, 0.221094165650959, 0.180608750801481, 0.12502377891255, 
0.269755048548634), distance = c(6.25093914350113, 5.90579526441211, 
3.35416246724451, 3.5986715069128, 3.74569894202013, 3.2687423704556, 
8.63358410851277, 9.12459363127179, 9.03891669576689, 3.70247933884298
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))`
  • 1
    Please share a few rows of `recdat` using `dput(head(recdat))`. Read: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – jay.sf Sep 26 '22 at 11:27
  • 1
    Greetings! It looks like your function is having issues with plugging in data frames. Usually it is helpful to provide a minimally reproducible dataset for questions here which some of us can plug into the function to see why yours didnt work. One way of doing this is by using the `dput` function. You can find out how to use it here: https://youtu.be/3EID3P1oisg – Shawn Hemelstrand Sep 26 '22 at 11:32
  • Thanks for the directions guys, guiding the newby! I have added some rows of recdat to the main question. – Jamie McKaughan Sep 26 '22 at 21:32

1 Answers1

0

It's unclear which values cause the issues - the ones you posted don't. Try looking at something like table(nchar(recdat$datetime)) to find any odd values first.

FWIW few comments: you can simply convert columns in place, e.g.:

convert.dates <- function(data, columns, format="%Y-%m-%d %H:%M:%S") {
  for (col in columns) data[[col]] <- strptime(data[[col]], format)
  data
}

and also dealing with POSIXlt is highly inefficient so I'd replace all of the above with

recdat$datetime <- as.POSIXct(recdat$datetime, "%Y:%m:%d %H:%M:%S")

which is much easier to compute with (you add seconds directly etc.).

Simon Urbanek
  • 13,842
  • 45
  • 45