[R] Function that uses a parameter to form an as.POSIXct function

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Thu Feb 26 17:05:23 CET 2026


Your proposed function's behaviour will be obscure to anyone else reading the code where you use it. 

Pools of named vectors are used throughout R because humans have difficulty mixing too many names in one place without running into problems with accidentally re-using names and wiping out useful information related to one part of the analysis with information related to another part. These "pools" come in different flavors: data frames, lists, named vectors and environments.

You seem to be assuming that you will always be working with data in the global environment... but this is one of the most at-risk places to have name collisions that will mess you up. For example, you might have one timestamp vector tracking when patients visited a doctor, and another one keeping track of times when they got sick. this is exactly the kind of situation where those should be in their own "pools" (data frames) because there will be other vectors that should line up with the time stamps.

It is better to have a function that accepts two parameters... one being the name of the object containing the data and the other being the name of that data. Also, assignment is one of the most clear operations someone reading the code can learn from, so burying it in a function hides the most useful and reusable/modifiable part of what you are doing. 

So,

readit <- function(data, name) {
  data[[ name ]]
}

can be re-used as

no[[ "Time" ]] <- readit( globalenv(), "Time" )

or

no[[ "Time" ]] <- readit( raw_data, "Time" )


On February 26, 2026 7:23:04 AM PST, "Sorkin, John" <jsorkin using som.umaryland.edu> wrote:
>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
>
>
>
>______________________________________________
>R-help using r-project.org 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.

--
Sent from my phone. Please excuse my brevity.
	[[alternative HTML version deleted]]



More information about the R-help mailing list