由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Statistics版 - pass arguments by reference in R
相关主题
R为什么写csv不能添加啊?mixed effects model 请教
another sas questionwhat is the fastest way of look-up in SAS?
an R questionR plot: 用pdf(append=T) 将多个图输出到一个PDF文件
再一次请教apply function 在R中的应用请教大牛,将多个dataset合并后,如何知道哪部分数据来自哪个dataset。
mixed linear model 里面fixed effect 和 random effect问几题sas adv, 多谢了!
谁能说说如何区分fixed effect, random effects在线等,垂直合并数据集
R lme random effect output, waiting online for response问个SAS的问题
弱问到底什么是fixed/random effect model?请教SAS输入法的问题
相关话题的讨论汇总
话题: ahash话题: myhash话题: alist话题: length话题: hh
进入Statistics版参与讨论
1 (共1页)
l******9
发帖数: 579
1
I need to do pass by reference in R by R studio on win 7.
My code:
myfunc<-function(myhash.arg, b)
{
myhash <- myhash.arg
if (!has.key("first", myhash))
myhash["first"] <- list()
alist <- myhash["first"]
alist <- append(alist, i)
eval.parent(substitute(myhash.arg<-myhash))
return(0)
}
ahash<-hash()
for(i in 1:5)
{
myfunc(myhash.arg = ahash, b = i)
print(c("length of ahash is ", length(ahash)))
print(c("length of ahash list is ", length(ahash["first"])))
}
but, the list size is always 1, the appended elements are missed.
Any help would be appreciated.
i*******r
发帖数: 51
2
You can pass the env handle to a function ,and change whatever you want.
However this will cause side effects in your program, and not be recommended.

【在 l******9 的大作中提到】
: I need to do pass by reference in R by R studio on win 7.
: My code:
: myfunc<-function(myhash.arg, b)
: {
: myhash <- myhash.arg
: if (!has.key("first", myhash))
: myhash["first"] <- list()
: alist <- myhash["first"]
: alist <- append(alist, i)
: eval.parent(substitute(myhash.arg<-myhash))

l******9
发帖数: 579
3
Could you please show me an example ?
what side effects ?
thanks

recommended.

【在 i*******r 的大作中提到】
: You can pass the env handle to a function ,and change whatever you want.
: However this will cause side effects in your program, and not be recommended.

i*******r
发帖数: 51
4
> env=environment();
> library(hash)
hash-2.2.6 provided by Decision Patterns
> hh=hash();
> has.key("a",hh)
a
FALSE
> myfun<-function(hhname,env){hh=env[[hhname]];if(!has.key("a",hh)){hh["a"]=
list()}else{append(hh["a"],1)}}
> myfun("hh",env)
> hh
containing 1 key-value pair(s).
a :
> myfun("hh",env)
[[1]]
containing 1 key-value pair(s).
a :
[[2]]
[1] 1

【在 l******9 的大作中提到】
: Could you please show me an example ?
: what side effects ?
: thanks
:
: recommended.

i*******r
发帖数: 51
5
Side effects means a function changed a value in its environment.
In functional programming language, (R is not pure fp, but designed with
lots of fp ideas), the side effects are considered as a problem, since
the function call does extra things more than compute the return value from
its inputs.

【在 l******9 的大作中提到】
: Could you please show me an example ?
: what side effects ?
: thanks
:
: recommended.

1 (共1页)
进入Statistics版参与讨论
相关主题
请教SAS输入法的问题mixed linear model 里面fixed effect 和 random effect
问个SAS数据处理的问题谁能说说如何区分fixed effect, random effects
问一个R的结果写到TXT文件问题R lme random effect output, waiting online for response
help need for SAS macro弱问到底什么是fixed/random effect model?
R为什么写csv不能添加啊?mixed effects model 请教
another sas questionwhat is the fastest way of look-up in SAS?
an R questionR plot: 用pdf(append=T) 将多个图输出到一个PDF文件
再一次请教apply function 在R中的应用请教大牛,将多个dataset合并后,如何知道哪部分数据来自哪个dataset。
相关话题的讨论汇总
话题: ahash话题: myhash话题: alist话题: length话题: hh