Re: Function that uses a parameter to form an as.POSIXct function

Rui Barradas <[email protected]> Thu, 26 Feb 2026 17:34:12 +0000
Newsgroups gmane.comp.lang.r.general
Message-ID <[email protected]>
Às 15:23 de 26/02/2026, Sorkin, John escreveu:
> I am trying to write a function which will take accept an argument and use the value of the argument to indicate the source of the data that should be processed in an as.POSIXct function. My attempt at writing the function is below:
> 
> readit <- function(species){
>    no[,"NewTime"] <- as.POSIXct(paste(species)[,"Time"],format=("%Y-%m-%d_%H:%M:%OS"),tz="UTC")
> }
> 
> If the value of species is no2, I want the function call
> readit("no2")
> to produce a statement:
> no[,"NewTime"] <- as.POSIXct(no[,"Time"],format=("%Y-%m-%d_%H:%M:%OS"),tz="UTC")
> 
> If the value of species is co2, I want the function call
> readit("co2")
> to produce a statement:
> no[,"NewTime"] <- as.POSIXct(co2[,"Time"],format=("%Y-%m-%d_%H:%M:%OS"),tz="UTC")
> 
> When I run either version of the function and the call to the function I receive the following error message:
> 
> Browse[1]> readit("no2")
> Error during wrapup: incorrect number of dimensions
> Error: no more error handlers available (recursive errors?); invoking 'abort' restart
> 
> Please help me fix my code
> 
> Thank you,
> 
> John
> 
> John David Sorkin M.D., Ph.D.
> Professor of Medicine, University of Maryland School of Medicine;
> Associate Director for Biostatistics and Informatics, Baltimore VA Medical Center Geriatrics Research, Education, and Clinical Center;
> Former PI Biostatistics and Informatics Core, University of Maryland School of Medicine Claude D. Pepper Older Americans Independence Center;
> Senior Statistician University of Maryland Center for Vascular Research;
> 
> Division of Gerontology, Geriatrics and Palliative Medicine,
> 10 North Greene Street
> GRECC (BT/18/GR)
> Baltimore, MD 21201-1524
> Cell phone 443-418-5382
> 
> 
> 
> ______________________________________________
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Hello,

This is very similar to other answers.



readit <- function(x, col = "Time") {
   new_col <- paste0("New", col)
   x[[new_col]] <- as.POSIXct(x[[col]],
                              format = "%Y-%m-%d_%H:%M:%S",
                              tz = "UTC")
   x
}

# data borrowed from Enrico's answer.
no <- data.frame("Time" = "2026-02-26_13:00:00")
co <- data.frame("Time" = "2026-02-25_13:00:00")

readit(no)
#>                  Time             NewTime
#> 1 2026-02-26_13:00:00 2026-02-26 13:00:00
readit(co)
#>                  Time             NewTime
#> 1 2026-02-25_13:00:00 2026-02-25 13:00:00


I thought the new column prefix could also be a function argument with 
default value "New".


readit <- function(x, col = "Time", prefix = "New") {
   new_col <- paste0(prefix, col)
   x[[new_col]] <- as.POSIXct(x[[col]],
                              format = "%Y-%m-%d_%H:%M:%S",
                              tz = "UTC")
   x
}


Hope this helps,

Rui Barradas