dimanche 20 mai 2018

How to list a number of objects by pattern in a brief way?

I'm looking for a way to abbreviate listing objects with similar name patterns. Consider this example.

vfoo <- "x"
v.1 <- v.2 <- v.n <- "foo"

Where n is the number of the last object.

> list(v.1, v.2, v.n)
[[1]]
[1] "foo"

[[2]]
[1] "foo"

[[3]]
[1] "foo"

The following only lists me the names of objects with specific pattern:

> list(ls(pattern="v."))
[[1]]
[1] "v.1" "v.2" "v.n"

Using this code I get the result I want:

> do.call("list", mget(grep("v.", names(.GlobalEnv), value=TRUE)))
$v.n
[1] "foo"

$v.1
[1] "foo"

$v.2
[1] "foo"

I benefit from this only when the number of objects to be listed exceeds a certain amount, though.

Might there be a brief solution like e.g. list(v.1 %to% v.n) (pseudo code) I do not know yet?

Aucun commentaire:

Enregistrer un commentaire