通过函数更新数据框

发布于 2025-02-11 12:34:00 字数 760 浏览 1 评论 0原文

我使用r…遇到了一个小问题……

在以下数据框架中,

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

我想在v1为1的行中更改v2的值。

test[test$v1==1,"v2"] <- 10

工作正常。

test
  v1 v2
1  1 10
2  1 10
3  1 10
4  2  0
5  2  0
6  2  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.fun(1)
  v1 v2
1  1 10
2  1 10
3  1 10
4  2  0
5  2  0
6  2  0

但是,当我现在看测试时:

test
  v1 v2
1  1  0
2  1  0
3  1  0
4  2  0
5  2  0
6  2  0

它不起作用。 是否有命令告诉R在功能中真正更新数据框架? 非常感谢您的任何帮助!

I ran into a little problem using R…

In the following data frame

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

I want to change values for v2 in the rows where v1 is 1.

test[test$v1==1,"v2"] <- 10

works just fine.

test
  v1 v2
1  1 10
2  1 10
3  1 10
4  2  0
5  2  0
6  2  0

However, I need to do that in a function.

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)
}

Calling the function seems to work.

test.fun(1)
  v1 v2
1  1 10
2  1 10
3  1 10
4  2  0
5  2  0
6  2  0

However, when I now look at test:

test
  v1 v2
1  1  0
2  1  0
3  1  0
4  2  0
5  2  0
6  2  0

it didn’t work.
Is there a command that tells R to really update the data frame in the function?
Thanks a lot for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

此生挚爱伱 2025-02-18 12:34:00

test在您的函数中是对象的 copy 从您的全局环境中(我假设这是定义的位置)。除非另有说明,否则分配发生在当前环境中,因此该函数内部发生的任何更改仅适用于功能内部的副本,而不适用于您的全局环境中的对象。

最好将所有必要的对象作为参数传递给函数。

就个人而言,我会在您的功能结束时返回(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

如果绝对有必要直接修改函数之外的对象,因此您需要告诉R,您想将test的本地副本分配给<test 代码> .globalenv 。

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

但是,使用分配&lt;&lt; -以这种方式并不常见,许多经验丰富的R程序员会建议反对它。

test in your function is a copy of the object from your global environment (I'm assuming that's where it is defined). Assignment happens in the current environment unless specified otherwise, so any changes that happen inside the function apply only to the copy inside the function, not the object in your global environment.

And it's good form to pass all necessary objects as arguments to the function.

Personally, I would return(test) at the end of your function and make the assignment outside of the function, but I'm not sure if you can do this in your actual situation.

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

If it is absolutely necessary to modify an object outside your function directly, so you need to tell R that you want to assign the local copy of test to the test in the .GlobalEnv.

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

Using assign or <<- in this fashion is fairly uncommon, though, and many experienced R programmers will recommend against it.

虐人心 2025-02-18 12:34:00

&lt; - 更改为&lt;&lt; - 在您的功能中,也可以做到这一点
请参阅 r-manual 。从该页面引用:

操作员&lt;&lt; - and - &gt;&gt;通常仅在函数中使用,并通过父环境进行搜索,以便对所分配的变量进行现有定义。如果找到了这样的变量(并且其绑定未锁定),则重新定义其值,否则在全球环境中进行分配。

然后,您的代码应该是:

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)

Changing the <- to <<- in your function, does the trick as well,
see the R-manual . Quote from that page:

The operators <<- and ->> are normally only used in functions, and cause a search to made through parent environments for an existing definition of the variable being assigned. If such a variable is found (and its binding is not locked) then its value is redefined, otherwise assignment takes place in the global environment.

Your code should then be:

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)
呆萌少年 2025-02-18 12:34:00

最好不要更改函数中的全局变量,因为这可能会使不可能的副作用。为了避免在R中,对函数内部的对象的任何更改实际上只会更改该函数环境>的本地副本

如果您真的想更改测试,则必须将函数的返回值分配为测试(最好以更明确的返回值编写功能,

 test <- test.fun(1)

或者选择要在test内部分配的全局环境。有趣

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

It is good practice to not change global variables in functions, because this may have undesirable side effects. To avoid this in R, any changes to objects inside a function actually only change copies that are local to that function's environment.

If you really want to change test, you have to assign the return value of the function to test (it would be better to write the function with a more explicit return value,

 test <- test.fun(1)

Or choose the global environment to assign to within test.fun,

test.fun <- function (x) {             
    test[test$v1==x,"v2"] <- 10             
    print(test)
    assign("test",test,.GlobalEnv)           
} 
原来是傀儡 2025-02-18 12:34:00

我认为这是由于评估的不同环境而发生的。您的函数副本test从全局环境中添加到临时的本地环境(在函数调用上创建),然后仅在此本地评估(即,更改)test环境。

您可以通过使用超级分配&lt; -来克服这个问题欺骗你,...)。

通常,约书亚·乌尔里希(Joshua Ulrich)提供的解决方案是解决这些问题的方式。您通过原始对象并将其返回。在功能上,您将结果分配给原始对象。

I think this happens because of the different environments that are evaluated. Your function copies test from the global environment into a temporary local environment (which is created on the function call) and then test is only evaluated (i.e., changed) in this local environment.

You could overcome this issue by using the super-assignment <<-, but this is NOT recommended and will lead to horrible unforeseen problems (your computer catches a virus, your girlfriend starts to cheat on you,...).

Generally the solution given by Joshua Ulrich is the way to go on these kind of problems. You pass the original object and return it. On function call you assign the result to your original object.

一指流沙 2025-02-18 12:34:00

您可以编写一个替换功能。这是一个名称以'&lt; - '结尾的函数,从本质上将其包装在:

foo = bar(foo)

包装器中。因此,在您的情况下:

> "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

请注意,您必须在创建或r上引用函数名称。

You could write a replacement function. This is a function with a name that ends in '<-' and essentially wraps it in a:

foo = bar(foo)

wrapper. So in your case:

> "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

Note you have to quote the function name on creation or R gets confused.

余生再见 2025-02-18 12:34:00

*我创建了一个称为read__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)

* I have created a function called read__csv I want to access that same data to other r function*

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文