R 通过函数'更新数据帧;行不通

R 通过函数'更新数据帧;行不通,r,function,dataframe,R,Function,Dataframe,我在使用R时遇到了一个小问题 在下面的数据框中 test <- data.frame(v1=c(rep(1,3),rep(2,3)),v2=0) 但是,我需要在函数中这样做 test <- data.frame(v1=c(rep(1,3),rep(2,3)),v2=0) test.fun <- function (x) { test[test$v1==x,"v2"] <- 10 print(test) } 然而,当我现在看测试时: test v

我在使用R时遇到了一个小问题

在下面的数据框中

test <- data.frame(v1=c(rep(1,3),rep(2,3)),v2=0) 
但是,我需要在函数中这样做

test <- data.frame(v1=c(rep(1,3),rep(2,3)),v2=0)

test.fun <- function (x) {
    test[test$v1==x,"v2"] <- 10
    print(test)
}
然而,当我现在看测试时:

test
  v1 v2
1  1  0
2  1  0
3  1  0
4  2  0
5  2  0
6  2  0
它不起作用。 是否有命令告诉R真正更新函数中的数据帧?
非常感谢你的帮助

test
函数中的对象是全局环境中对象的副本(我假设它是在全局环境中定义的)。除非另有规定,否则赋值在当前环境中进行,因此函数内部发生的任何更改仅应用于函数内部的副本,而不是全局环境中的对象

将所有必要的对象作为参数传递给函数是一种很好的形式

test <- data.frame(v1=c(rep(1,3),rep(2,3)),v2=0)

test.fun <- function (x) {
    test[test$v1==x,"v2"] <- 10
    print(test)
}
就我个人而言,我会在函数结束时返回(测试),并在函数之外进行赋值,但我不确定您在实际情况下是否可以这样做

test.fun <- function (x, test) {
    test[test$v1==x,"v2"] <- 10
    return(test)
}
test <- data.frame(v1=c(rep(1,3),rep(2,3)),v2=0)
(test <- test.fun(1, test))
#  v1 v2
#1  1 10
#2  1 10
#3  1 10
#4  2  0
#5  2  0
#6  2  0

使用
assign
时,最好不要更改函数中的全局变量,因为这可能会产生不需要的结果。为了避免在R中出现这种情况,对函数内部对象的任何更改实际上只会更改该函数的
环境的本地副本

如果您确实想更改test,则必须将函数的返回值指定给test(最好使用更显式的返回值编写函数

 test <- test.fun(1)

我认为发生这种情况的原因是所评估的
环境不同。您的函数将
test
从全局环境复制到临时本地环境(在函数调用时创建),然后
test
仅在此本地环境中评估(即更改)


您可以通过使用超级赋值
来解决此问题。您可以编写一个替换函数。此函数的名称以“更改*结尾。我创建了一个名为read\u csv的函数,我想将相同的数据访问到其他r函数*

read__csv <- function(files_csv) {
  print(files_csv)
  # set R workign directory as current R file path
  setwd(system("pwd", intern = T) )
  print( getwd() )
  data<-read.csv(files_csv,header = TRUE,na.strings=0)
  print(data)
  assign("data", data, envir = .GlobalEnv)
 #create data varible to r global envrioment 
}

#R Funtion calling
read__csv("csv.csv")

print(data)
read\uu csv
test.fun <- function (x, test) {
    test[test$v1==x,"v2"] <- 10
    assign('test',test,envir=.GlobalEnv)
    #test <<- test  # This also works, but the above is more explicit.
}
(test.fun(1, test))
#  v1 v2
#1  1 10
#2  1 10
#3  1 10
#4  2  0
#5  2  0
#6  2  0
 test <- test.fun(1)
test.fun <- function (x) {             
    test[test$v1==x,"v2"] <- 10             
    print(test)
    assign("test",test,.GlobalEnv)           
} 
> "setV2<-" = function (x,value,m){x[x$v1==m,"v2"]=value;return(x)}
> test <- data.frame(v1=c(rep(1,3),rep(2,3)),v2=0) 
> setV2(test,1)=10
> test
  v1 v2
1  1 10
2  1 10
3  1 10
4  2  0
5  2  0
6  2  0
> setV2(test,2)=99
> test
  v1 v2
1  1 10
2  1 10
3  1 10
4  2 99
5  2 99
6  2 99
test <- data.frame(v1=c(rep(1,3),rep(2,3)),v2=0) 

test.fun <- function (x) {
  test[test$v1==x,"v2"] <<- 10
  print(test)
}

test.fun(1)
read__csv <- function(files_csv) {
  print(files_csv)
  # set R workign directory as current R file path
  setwd(system("pwd", intern = T) )
  print( getwd() )
  data<-read.csv(files_csv,header = TRUE,na.strings=0)
  print(data)
  assign("data", data, envir = .GlobalEnv)
 #create data varible to r global envrioment 
}

#R Funtion calling
read__csv("csv.csv")

print(data)