Reputation: 415
I am creating a package that has a function which returns a list of several names (weight_, height_, bmi_
) with each name repeated suffixed by a number from 0 to 50. For example, the function would return a list "weight_0"..."weight_50"
, "height_0"..."height_50", "bmi_0"..."bmi_50".
How can I nest another function within my function below that creates these new names without having to list them all out
myfunction <- local(function() {
return(c("weight_0", "height_0", "bmi_0"
))
})
Upvotes: 0
Views: 30
Reputation: 51582
You can also try outer
, i.e.
as.vector(outer(outs, 0:50, paste, sep = '_'))
Upvotes: 2
Reputation: 2467
You can achieve this using the following code
outs = c("weight", "height", "bmi")
unlist(lapply(outs,function(x) paste0(x,"_",0:50)))
Here we paste each element in outs
to the numbers from 0 to 50. This creates a list with a vector for each of the elements in outs
, which we then unlist to a single vector.
An alternative (may be faster):
paste0(rep(outs,each=51),"_",rep(0:50))
Upvotes: 1