Sorkin, John
2026-Feb-26 15:23 UTC
[R] Function that uses a parameter to form an as.POSIXct function
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
Enrico Schumann
2026-Feb-26 15:46 UTC
[R] Function that uses a parameter to form an as.POSIXct function
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, > > JohnYou 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
Jeff Newmiller
2026-Feb-26 16:05 UTC
[R] Function that uses a parameter to form an as.POSIXct function
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 at
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 at 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]]
Rui Barradas
2026-Feb-26 17:34 UTC
[R] Function that uses a parameter to form an as.POSIXct function
?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 > > > > ______________________________________________ > R-help at 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.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
@vi@e@gross m@iii@g oii gm@ii@com
2026-Feb-26 23:12 UTC
[R] Function that uses a parameter to form an as.POSIXct function
Hohn,
It may be that you are using a different attempt to specify where you are
putting the result than I might.
Before the code is run, you do not have a variable called "no" and it
is not
a dataframe just because you put something in it. Ideally, you create a
data.frame of the right number of rows first. The length of the species
variable , as in nrow(species) may be what you want to initialize with.
Then, I personally might prefer the dollar sign notation as in:
No$NewTime <-
as.POSIXct(paste(species)[,"Time"],format=("%Y-%m-%d_%H:%M:%OS"),tz="UTC")
I think this part is scrambled:
as.POSIXct(paste(species)[,"Time"],format=("%Y-%m-%d_%H:%M:%OS"),tz="UTC")
It looks like you want to take a column from the pass-in variable species
which I would guess is a data.frame. As before, I think a simpler way to say
what you want is species$Time and you want to get the current time as a
formatted string and append it to each item in that vector. Is that right?
Well, you may have switched the order of using paste and as.POSIXct.
The as.POSIXct function takes two arguments. The first is an object to be
converted and the second is a format. There are other optional arguments but
the first argument should be something like "2011-03-27 01:30:00". I
assume
you only need to take this time snapshot once BEFORE tring to patch the same
string to the end of each of the contents in species.
So you want to create one variable and reuse it in a paste. What you asked
for above is badly formatted and even if you want to keep the in-line
version above, it should look like this in a formatted version that makes it
clearer.
no[,"NewTime"] <-
paste(species$Time,
as.POSIXct(SOMETHING,
format=("%Y-%m-%d_%H:%M:%OS"),tz="UTC")
)
Sorry, but this is not a constant width font.
Note I added the word SOMETHING above. That should be replaced by either a
fixed date in the expected format or a cll to the operating system to
deliver the date and time.
Ideally there are no spaces in the output .
The goal is to make one such date string and paste will concatenate the same
copy to each of the items you have. Be careful as paste by default includes
a space and I supect you do not want one. Here is an example:
> temp = c("a", "b", "c")
> paste(temp, ".suf")
[1] "a .suf" "b .suf" "c .suf"
> paste(temp, ".suf", sep="")
[1] "a.suf" "b.suf" "c.suf"
When doing unfamiliar things, it is easier to build smaller pieces and
verify on examples that they do what you want and then keep going and maybe
at the end, rewrite it.
-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of Sorkin, John
Sent: Thursday, February 26, 2026 10:23 AM
To: r-help at r-project.org (r-help at r-project.org) <r-help at
r-project.org>
Subject: [R] Function that uses a parameter to form an as.POSIXct function
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 at 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.