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 <[email protected]> On Behalf Of Sorkin, John
Sent: Thursday, February 26, 2026 10:23 AM
To: [email protected] ([email protected]) <[email protected]>
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
______________________________________________
[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.
______________________________________________
[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.