通过函数更新数据框
我使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
test
在您的函数中是对象的 copy 从您的全局环境中(我假设这是定义的位置)。除非另有说明,否则分配发生在当前环境中,因此该函数内部发生的任何更改仅适用于功能内部的副本,而不适用于您的全局环境中的对象。最好将所有必要的对象作为参数传递给函数。
就个人而言,我会在您的功能结束时
返回(test)
,并在功能之外进行分配,但是我不确定您是否可以在实际情况下执行此操作。如果绝对有必要直接修改函数之外的对象,因此您需要告诉R,您想将
test
的本地副本分配给<test
代码> .globalenv 。但是,使用
分配
或&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.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 thetest
in the.GlobalEnv
.Using
assign
or<<-
in this fashion is fairly uncommon, though, and many experienced R programmers will recommend against it.将&lt; - 更改为&lt;&lt; - 在您的功能中,也可以做到这一点
请参阅 r-manual 。从该页面引用:
然后,您的代码应该是:
Changing the <- to <<- in your function, does the trick as well,
see the R-manual . Quote from that page:
Your code should then be:
最好不要更改函数中的全局变量,因为这可能会使不可能的副作用。为了避免在R中,对函数内部的对象的任何更改实际上只会更改该函数
环境>的本地副本
。如果您真的想更改测试,则必须将函数的返回值分配为测试(最好以更明确的返回值编写功能,
或者选择要在
test内部分配的全局环境。有趣
,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,
Or choose the global environment to assign to within
test.fun
,我认为这是由于评估的不同
环境
而发生的。您的函数副本test
从全局环境中添加到临时的本地环境(在函数调用上创建),然后仅在此本地评估(即,更改)test
环境。您可以通过使用超级分配
&lt; -
,来克服这个问题欺骗你,...)。通常,约书亚·乌尔里希(Joshua Ulrich)提供的解决方案是解决这些问题的方式。您通过原始对象并将其返回。在功能上,您将结果分配给原始对象。
I think this happens because of the different
environments
that are evaluated. Your function copiestest
from the global environment into a temporary local environment (which is created on the function call) and thentest
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.
您可以编写一个替换功能。这是一个名称以'&lt; - '结尾的函数,从本质上将其包装在:
foo = bar(foo)
包装器中。因此,在您的情况下:
请注意,您必须在创建或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:
Note you have to quote the function name on creation or R gets confused.
*我创建了一个称为read__csv的函数,我想访问相同的数据到其他R函数*
* I have created a function called read__csv I want to access that same data to other r function*