Remi.b Remi.b - 1 year ago 94
R Question

Append function that assign new value

I am trying to create a function

that does not return a value but directly extend the first variable. Currently, to append
I do

x = append(x,y)

I would like to be able to do


and get the same result. I first thought of something like

Append = function(a,b,VarName) assign(VarName,append(a,b), envir = .GlobalEnv)

It works but it is quite unsatisfying to have to pass the name of the original variable. Is there a better solution?

Answer Source

Since you're doing this to learn, maybe a more R-like approach to in-place modification is a replacement function

`append_to<-` = function(x, ..., value) append(x, value)

used as

x = 1:5
append_to(x) <- 5:1

resulting in

> x
 [1] 1 2 3 4 5 5 4 3 2 1
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download