מניפולציות על קריאות לפנוקציות בR

15 views
Skip to first unread message

Harel Lustiger

unread,
Feb 18, 2015, 4:01:47 AM2/18/15
to israel-r-...@googlegroups.com

שבוע טוב לכולם

אני מנסה לבנות מעטפת לפונקציה הלוקחת ארגומנטים ומכניסה אותם לפונקציה בתוכה. ךדוגמה, נבנה פונקצית rnormal שעוטפת ומבצעת (על הנייר) בדיוק מה שעושה פונקציה rnorm.
ספוליר: לא משנה איזה n נכניס לפנוקציה נקבל תמיד 3 מ”מ.
השאלה איך גורמים לכך שברגע שמכניסים, למשל n=200 נקבל 200 מ”מ. (כמובן שזאת דוגמאת צעצוע, בפועל על הפונקציה האמיתית לא ניתן להריץ לולאה \ XXapply).
תודה לעונים.

First implementation

rnormal <- function(n,mean=0,sd=1){
        rnorm(as.list(match.call())[-1])      
}
set.seed(2015)
rnormal(n=200,0,1)

[1] -1.5454484 -0.5283932 -1.0867588

Second implementation

rnormal <- function(n,mean=0,sd=1){
        rnorm(mget(names(formals()),sys.frame(sys.nframe())))      
}
set.seed(2016)
rnormal(n=200,0,1)

[1] -0.91474184 1.00124785 -0.05642291

set.seed(2016)
rnormal(n=1,0,1)

[1] -0.91474184 1.00124785 -0.05642291

Tal Galili

unread,
Feb 18, 2015, 4:06:10 AM2/18/15
to israel-r-...@googlegroups.com
ייתכן ולא הבנתי את השאלה.
בעיקר הפתרון הוא לעשות את הדבר הבא:
rnormal <- function(n,mean=0,sd=1){
        rnorm(n =n, mean = mean, sd = sd)      
}
set.seed(2016)
rnormal(n=200,0,1)

האם זה עונה לשאלתך, או שפיספסתי פה משהו?




----------------Contact Details:-------------------------------------------------------
Contact me: Tal.G...@gmail.com
Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English)
----------------------------------------------------------------------------------------------


--
You received this message because you are subscribed to the Google Groups "Israel R User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-g...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Harel Lustiger

unread,
Feb 18, 2015, 4:06:40 AM2/18/15
to israel-r-...@googlegroups.com

\\\

ועכשיו לבעלי מערכות ההפעלה בעברית

Liad Shekel

unread,
Feb 18, 2015, 4:07:42 AM2/18/15
to Israel R User Group
rnormal <- function(n,mean=0,sd=1){
  arg.list <- as.list(match.call())[-1]
  do.call(rnorm, arg.list)
}


2015-02-18 11:01 GMT+02:00 Harel Lustiger <ha...@loostiger.com>:

--
You received this message because you are subscribed to the Google Groups "Israel R User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-g...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
:-)

Liad Shekel

unread,
Feb 18, 2015, 4:12:00 AM2/18/15
to Israel R User Group
אגב, לא ברור לי מה הצרכים שלך, אבל שווה להכיר את ... (שלוש נקודות)
תוצאה ראשונה בגוגל מובליה לפה, אבל אני מניח שאתה יכול למצוא עוד מקורות.

--
:-)

Harel Lustiger

unread,
Feb 18, 2015, 4:13:43 AM2/18/15
to israel-r-...@googlegroups.com
תודה טל.
תודה ליעד, בדיוק השורה שהייתה חסרה בקוד.

Reply all
Reply to author
Forward
0 new messages