R中函数内的对象重新分配

geo*_*ory 0 r

稍微尴尬地问这么简单的问题,但我现在浪费了一个小时,并计算出30秒的解决方案.问题是如何编辑作为函数输入提供的现有对象.我也玩过超级任务<<-但没有成功.

示例函数使用2个输入(一个用于对象,一个用于其名称).我只需要一种不需要'n'输入的形式.

m <- c(2,5,3,7,1,3,9,3,5)
dim(m) <- c(3,3)
m

f <- function(x, n) { # where 'n' is the object name of 'x'
  x[1,] <- c(1,2,3)
  assign(n, x, envir = .GlobalEnv)
}

f(m, 'm')
m
Run Code Online (Sandbox Code Playgroud)

提前致谢.

geo*_*ory 5

好的解决了.谢谢@Andrie,抱歉,我误解了你的回复.

菜鸟错误:(

f <- function(x) {
  x[1,] <- c(1,2,3)
  return(x)
}

m <- f(m)
m
Run Code Online (Sandbox Code Playgroud)