我试图创建一个Append不返回值但直接扩展第一个变量的函数.目前,要追加y到x我做的
x = append(x,y)
Run Code Online (Sandbox Code Playgroud)
我希望能够做到
Append(x,y)
Run Code Online (Sandbox Code Playgroud)
并得到相同的结果.我首先想到的是类似的东西
Append = function(a,b,VarName) assign(VarName,append(a,b), envir = .GlobalEnv)
Append(x,y,"x")
Run Code Online (Sandbox Code Playgroud)
它有效,但必须传递原始变量的名称是非常不满意的.有更好的解决方案吗?
既然你要这样做是为了学习,也许更像R的方法来进行就地修改是一种替代功能
`append_to<-` = function(x, ..., value)
append(x, ..., values=value)
Run Code Online (Sandbox Code Playgroud)
用作
x = 1:5
append_to(x) <- 5:1
y = 1:5
append_to(y, after=3) <- c(3:1, 1:3)
Run Code Online (Sandbox Code Playgroud)
导致
> x
[1] 1 2 3 4 5 5 4 3 2 1
> y
[1] 1 2 3 3 2 1 1 2 3 4 5
Run Code Online (Sandbox Code Playgroud)