由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Statistics版 - R doesnt have pass by reference mechanism?
相关主题
R 里面 可以定义 C 类似 的 reference 变量吗?Huge speed increase in R for scalar intensive computation
R: 变object 名为 string辅修stats或biostats的问题
Does R function pass parementer by reference or by copy?help, got laid off
Python - scraping 统计版 - 如何翻页?R question
[合集] 求助 R function hist咣,咣,咣,上书了!Quick R guide.
[R] How to apply "apply" here?请教一个概率题的思路
SAS Macro Function怎么演化成这样了呢?
请教一个R的问题(function)也谈什么是统计
相关话题的讨论汇总
话题: reference话题: function话题: pass话题: func1话题: passing
进入Statistics版参与讨论
1 (共1页)
D******n
发帖数: 2836
1
You cant pass parameters by reference to functions in R?
That sucks...
f***a
发帖数: 329
2
reference mechanism是啥?可能展开讲解下
D******n
发帖数: 2836
3
basically it means if u pass an array to a function ,for example
func1(x)
x is an array or vector
the values of elements of x can be changed in the function, and the changes
remain after the function call is finished
example,
func1(x)
{ x[1]<- 0;
}
x<-c(1,2,3,4)
func1(x)
x
0,2,3,4

【在 f***a 的大作中提到】
: reference mechanism是啥?可能展开讲解下
s*****n
发帖数: 2174
4
use <<-
global assignment.
> func1 <- function(x) {x[1]<<- 0}
> x<-c(1,2,3,4)
> func1(x)
> print(x)
[1] 0 2 3 4
>
D******n
发帖数: 2836
5
It is a workaround ,but it is not really pass by reference and it is so
clumsy

【在 s*****n 的大作中提到】
: use <<-
: global assignment.
: > func1 <- function(x) {x[1]<<- 0}
: > x<-c(1,2,3,4)
: > func1(x)
: > print(x)
: [1] 0 2 3 4
: >

s*****n
发帖数: 2174
6
why do you need passing by reference?
for performance reason or something else?
Actually, when you pass a parameter into a function, the parameter is not
copied, unless it is modified, so what's the need for passing by reference?
D******n
发帖数: 2836
7
Because it needs to be modified.
when u pass a vector to a function, u want the function to modify its elemen
ts. It is a very common senario.Easy to implement in all other common langua
ges but not in R.

【在 s*****n 的大作中提到】
: why do you need passing by reference?
: for performance reason or something else?
: Actually, when you pass a parameter into a function, the parameter is not
: copied, unless it is modified, so what's the need for passing by reference?

q**j
发帖数: 10612
8
Passing by reference is only necessary when you need to speed things up and
save memory. Otherwise, you can always type a couple of extra lines to
achieve the same effect. as a script language, my understanding is that R
team has made the decision to trade speed for ease of coding etc. So it is
simply the consequence of this decision. if you are absolutely crazy about
speed, you might not want to use R at the first place. so there is nothing
you can really complain about. just my humble opioion.
D******n
发帖数: 2836
9
Actually i am raising this question because there is recursion in my functionon.
f<-function(x)
{ ...
x[1]<<-x[2];
f(x)
...
}
then if u type f(x), it might work
but f(a) it doesn't work
so i have to make a wrapper function outside it.anyway, this is not a problem now, cause i have this workaround. And maybe it is right, for most of the time, we dont necessarily need(or can avoid) pass by reference.But i just feel uncomfortable to type extra lines and to use an inconvenient angle to look a

【在 q**j 的大作中提到】
: Passing by reference is only necessary when you need to speed things up and
: save memory. Otherwise, you can always type a couple of extra lines to
: achieve the same effect. as a script language, my understanding is that R
: team has made the decision to trade speed for ease of coding etc. So it is
: simply the consequence of this decision. if you are absolutely crazy about
: speed, you might not want to use R at the first place. so there is nothing
: you can really complain about. just my humble opioion.

s*****n
发帖数: 2174
10
If you just need to modify the arguments, you can use <<-.
This is not a reason to complain. This is not a workaround. In some
extent, passing by reference is a workaround of global assignment.
The only valid reason I can think about is the performance reason,
say passing parameter by reference probably saves time and space.
But it is not a problem for R either, because R does not copy the
parameter when it is passed into a function. However, you do need
to be careful when you do this
y <- myfun

【在 D******n 的大作中提到】
: Because it needs to be modified.
: when u pass a vector to a function, u want the function to modify its elemen
: ts. It is a very common senario.Easy to implement in all other common langua
: ges but not in R.

相关主题
[R] How to apply "apply" here?Huge speed increase in R for scalar intensive computation
SAS Macro Function辅修stats或biostats的问题
请教一个R的问题(function)help, got laid off
进入Statistics版参与讨论
D******n
发帖数: 2836
11
Thanks for your input, but please take a look at post 12297.
It is really a workaround in that scenario.

【在 s*****n 的大作中提到】
: If you just need to modify the arguments, you can use <<-.
: This is not a reason to complain. This is not a workaround. In some
: extent, passing by reference is a workaround of global assignment.
: The only valid reason I can think about is the performance reason,
: say passing parameter by reference probably saves time and space.
: But it is not a problem for R either, because R does not copy the
: parameter when it is passed into a function. However, you do need
: to be careful when you do this
: y <- myfun

s*****n
发帖数: 2174
12
I did read your post, but I do not understand why recursion causes a problem
here. What do you mean by f(x) works but f(a) doesn't?
Can you give a concrete example:
(1) What do you plan to do
(2) what is the expected (desired) output?
(3) What does R output due to the lack of passing by reference?
D******n
发帖数: 2836
13
Actually it is not limited to recurrsion, try the following example.
f<-function(x)
{ x[1]<<-1;
}
a<-c(0,1,2);
f(a);
cat(a,"\n");
x<-c(0,1,2);
f(x);
cat(x,"\n");

problem

【在 s*****n 的大作中提到】
: I did read your post, but I do not understand why recursion causes a problem
: here. What do you mean by f(x) works but f(a) doesn't?
: Can you give a concrete example:
: (1) What do you plan to do
: (2) what is the expected (desired) output?
: (3) What does R output due to the lack of passing by reference?

O*****y
发帖数: 222
14
Really great!
Songkun, could you tell me where do you learn these? I'm using R to process
some large data sets, knowing these knowledge can make my code more
efficient. Thanks!

【在 s*****n 的大作中提到】
: If you just need to modify the arguments, you can use <<-.
: This is not a reason to complain. This is not a workaround. In some
: extent, passing by reference is a workaround of global assignment.
: The only valid reason I can think about is the performance reason,
: say passing parameter by reference probably saves time and space.
: But it is not a problem for R either, because R does not copy the
: parameter when it is passed into a function. However, you do need
: to be careful when you do this
: y <- myfun

s*****n
发帖数: 2174
15
This is relevant to some details about R's philosophy, lexical scope.
You can achieve it by using "substitute", for example:
g <- function(x){
tmp <- substitute(expression(x[1] <<- x[1]+1))
eval(eval(tmp))
}
x <- c(0,1,2); g(x)
print(x) # gives you 1,1,2
a <- c(0,1,2); g(a)
print(a) # gives you 1,1,2
But, it is pretty messy though.

【在 D******n 的大作中提到】
: Actually it is not limited to recurrsion, try the following example.
: f<-function(x)
: { x[1]<<-1;
: }
: a<-c(0,1,2);
: f(a);
: cat(a,"\n");
: x<-c(0,1,2);
: f(x);
: cat(x,"\n");

D******n
发帖数: 2836
16
在 songkun (告别棒球场) 的大作中提到: 】
ya, thats what i meant.
You can do it but it looks ugly.
But i guess most of the time we dont come across this problem or we can copy
the varaible
g<-function(x)
{ a<-x;
a[1]<-1;
return(a);
}
a <- c(0,1,2),
a<-g(a);
cat(a);
but this is very slow. and yes, we can use your method.
Anyway, if we can use pass by reference in R, it would be much more straight
forward . Even perl has pass by reference, although as far as i know matlab
doesnt have pass by reference eithe
s*****n
发帖数: 2174
17
Yes, that's right.
Passing by reference are usually achieved by using pointers. R does not
allow pointers. Pointer operation is useful in generic programming but not
so much in statistical computation in comparison to being easy to use.
One interesting platform is Python, where everything is virtually passed by
reference, although no pointers are allowed.
1 (共1页)
进入Statistics版参与讨论
相关主题
也谈什么是统计[合集] 求助 R function hist
再写一个找工作的经历[R] How to apply "apply" here?
R program helpSAS Macro Function
How to transpose a data frame in R请教一个R的问题(function)
R 里面 可以定义 C 类似 的 reference 变量吗?Huge speed increase in R for scalar intensive computation
R: 变object 名为 string辅修stats或biostats的问题
Does R function pass parementer by reference or by copy?help, got laid off
Python - scraping 统计版 - 如何翻页?R question
相关话题的讨论汇总
话题: reference话题: function话题: pass话题: func1话题: passing