On Thu, 26 Feb 2026, Sorkin, John writes:

> 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

You could put all your data into a list, like here:

    Data <- list(no = data.frame("Time" = "2026-02-26_13:00:00"),
                 co = data.frame("Time" = "2026-02-25_13:00:00"))

Now you you can access specific data.frames from that list
(but I'd pass the list as an argument):

    readit <- function(species, Data){
        as.POSIXct(Data[[species]][, "Time"],
                   format = "%Y-%m-%d_%H:%M:%OS",
                   tz = "UTC")
    }

    readit("co", Data)
    ## [1] "2026-02-25 13:00:00 UTC"
    readit("no", Data)
    ## [1] "2026-02-26 13:00:00 UTC"

These values could then be assigned to another
data.frame 'no2', say:

    no2[, "NewTime"] <- readit("no", Data)


(You would probably get more/better help if you posted
 a reproducible example, with a small example dataset
 and the desired result.)


kind regards
    Enrico
    
> 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

-- 
Enrico Schumann
Lucerne, Switzerland
https://enricoschumann.net

______________________________________________
[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.

Reply via email to