通过功能更新数据帧不起作用 [英] Update data frame via function doesn't work

查看:120
本文介绍了通过功能更新数据帧不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用R ...遇到一个小问题



在以下数据框架中

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

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

 code> 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 v1 == x,v2]< - 10
打印(测试)
}

调用函数似乎起作用。

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

然而,w我现在看看测试:

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

它没有工作。
是否有一个命令告诉R真正更新函数中的数据框?
非常感谢任何帮助!

解决方案

test 在您的函数中,来自您的全局环境的对象的一个​​复制(我假设这是它被定义的位置)。分配发生在当前环境中,除非另有规定,因此您需要告诉R,您要将 test 的本地副本分配给 test .GlobalEnv 。



将所有必需的对象作为参数传递,这是一个很好的形式函数。

  test.fun<  -  function(x,test){
test [test $ v1 = = x,v2]< - 10
assign('test',test,envir = .GlobalEnv)
#test<< - 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

个人而言,我将 return(test)并将该作业分配到该函数之外,但不确定您是否可以在实际情况下执行此操作。 / p>

  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


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!

解决方案

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 you need to tell R that you want to assign the local copy of test to the test in the .GlobalEnv.

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

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

Personally, I would return(test) 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

这篇关于通过功能更新数据帧不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆