Good day, Is it possible to somehow specify more complex return types, such as a data.frame with specific columns? -------------------------------------- Dario Strbenac University of Sydney Camperdown NSW 2050 Australia
[R-pkg-devel] vapply and More Complex FUN.VALUE
3 messages · Dario Strbenac, Ivan Krylov, Uwe Ligges
On Tue, 26 Sep 2023 20:00:03 +0000
Dario Strbenac <dstr7320 at uni.sydney.edu.au> wrote:
Is it possible to somehow specify more complex return types, such as a data.frame with specific columns?
Probably not with vapply(). It only looks at the equivalent of typeof(), verifies the length() (which for data.frames is the number of columns) and then combines the objects along the axis that you're not interested in (building a list-matrix). I've been using the do.call(rbind, lapply(...)) idiom, relying on rbind.data.frame to check its arguments. It could probably be made more efficient, but it does the job.
Best regards, Ivan
On 27.09.2023 09:35, Ivan Krylov wrote:
On Tue, 26 Sep 2023 20:00:03 +0000 Dario Strbenac <dstr7320 at uni.sydney.edu.au> wrote:
Is it possible to somehow specify more complex return types, such as a data.frame with specific columns?
Probably not with vapply(). It only looks at the equivalent of typeof(), verifies the length() (which for data.frames is the number of columns) and then combines the objects along the axis that you're not interested in (building a list-matrix). I've been using the do.call(rbind, lapply(...)) idiom, relying on rbind.data.frame to check its arguments. It could probably be made more efficient, but it does the job.
Or perhaps you simply look for defining a new class (I'd use S3) where the output is a specific data frame (with some prefedined columns) to which you assign a class attribute? Best, Uwe Ligges