Write, save, and load functions

35 views
Skip to first unread message

Ayala Allon

unread,
May 5, 2013, 10:13:29 AM5/5/13
to israel-r-...@googlegroups.com
שלום לכולם,
 
אני משתמשת חדשה בתוכנה אז אני מתנצלת מראש על השאלות של מתחילים שבטח יהיו לי....
 
כתבתי את הפונקציה הבאה:
 
mac_fun <- function(ac_vector) {
mac_calc<-(sum(ac_vector)/length(ac_vector))
return(mac_calc)
}
 
 
המטרה היא בעצם לחשב דיוק. אם למישהו יש רעיון טוב יותר לפונקציה שעושה את זה אני אשמח לשמוע.
 
לאחר מכן שמרתי את הפונקציה
 
save(mac_fun, file="mac_fun.fun")
 
 
השאלות שלי:
 כדי להשתמש בפונקציה שכתבתי האם אני צריכה להעלות את הפונקציה, כלומר להשתמש ב
load()
או שמספיק שאני נמצאת בתיקייה איפה ששמרתי את הפונקציה?
כלומר איך אני מעלה מחדש ומשתמשת בפונקציה שכתבתי
 
השאלה השנייה שלי היא כזאת: אני רוצה להשתמש בפקודה 
tapply
יחד עם הפונקציה שכתבתי. כלומר במקום להשתמש
tapply
יחד עם הפונקציה
mean
אני רוצה להשתמש ב-
tapply
יחד עם הפונקציה החדשה שכתבתי.
 
 
תודה רבה על העזרה
אילה
 
 

amit gal

unread,
May 5, 2013, 10:18:41 AM5/5/13
to israel-r-...@googlegroups.com
שלום,

כדי להשתמש בפונקציה את צריכה לקרוא את הקובץ כל פעם שאת מתחילה סשן חדש. כלומר להשתמש ב
load("mac_fun.fun")
דרך פשוטה לראות אם הפונקציה זמינה לך היא פשוט להקיש את שמה ולראות אם R צועק שהוא לא יודע על מה את מדברת. אם הוא לא יודע, אז צריך להעלות את הקובץ.
מרגע שהפונקציה זמינה, משתמשים בה בדיוק כמו כל פונקציה אחרת
tapply(my.vector,list(my.tags),mac_fun)




2013/5/5 Ayala Allon <ayala...@gmail.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/groups/opt_out.
 
 

Tal Galili

unread,
May 5, 2013, 10:19:05 AM5/5/13
to israel-r-...@googlegroups.com
שלום אילה,

1) אני לא בטוח שאני מבין את מטרת הפונקציה, אני לא יודע מה זה אומר "לחשב דיוק" (לחשב דיוק של מה?!)

2) אכן, אם את רוצה לטעון בחזרה אובייקט לסביבת העבודה שלך (בזמן שבו את פותחת את R מחדש), יהיה עליך להשתמש ב load.

3) אם מדובר רק באובייקט יחיד, תסתכלי על  saveRDS

4) בפקודת ה- tapply יש ארגומנט שבו את מכניס את שם האובייקט של הפונקציה אשר כתבת.

בברכה,
טל






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



2013/5/5 Ayala Allon <ayala...@gmail.com>
 
 

--

amit gal

unread,
May 5, 2013, 10:37:55 AM5/5/13
to israel-r-...@googlegroups.com
אני תוהה.... האם הפונקציה שלך לא עושה פשוט ממוצע?


2013/5/5 Ayala Allon <ayala...@gmail.com>
 
 

--

Ayala Allon

unread,
May 6, 2013, 4:49:28 AM5/6/13
to israel-r-...@googlegroups.com

היי

אני רוצה לחשב דיוק באופן הבא: יש לי וקטור של 0 ו-1, כאשר 1 הכוונה לתשובה נכונה ו-0 הכוונה לתשובה לא נכונה. אז לפי מה שאני מבינה אני צריכה:

1.  לדעת את האורך של הוקטור.

2. לסכום את הוקטור.

3. לחלק את סכום הוקטור באורך הוקטור.

 

זה מה שייתן לי דיוק באופן שבו אני רוצה לחשב דיוק.

 

הפונקציה כן כתובה נכון, בדקתי אותה והיא עובדת :) .

 

העניין הוא שכאשר אני משתמשת בפונקציה בתוך tapply אני לא יכולה לשנות את ac_vector של הפונקציה שכתבתי, אלא אם כן שיניתי אותו מראש, כמו למשל בדוגמא הבאה:

 

ac_vector = ac_data$ac

 

mac_fun <- function(ac_vector) {

mac_calc<-(sum(ac_vector)/length(ac_vector))

return(mac_calc)}

 

mac = tapply(ac_data$ac,list(ac_data$subject,ac_data$condition),mac_fun) 

 

 האם יש דרך שבה אני אוכל לשלב את השורה הזאת

ac_vector = ac_data$ac

בשאר הקוד שכתבתי? אשמח להצעות והכוונה.

תודה רבה

אילה

To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-group+unsub...@googlegroups.com.

Tal Galili

unread,
May 6, 2013, 8:11:17 AM5/6/13
to israel-r-...@googlegroups.com
שלום אילה,

במקור חשבתי שהתכוונת למקרה הכללי של שימוש באובייקטי פונקציה, אך ממה שכתבת נשמע שאת מחפשת לחשב ממוצע. במקרה כזה (ותקני אותי אם לא הבנתי נכון), ל- R כבר יש פונקציה לזה (בשם השימושי "mean").

בכל מקרה, מאיך שכתבת את הקוד, זה אמור לעבוד כמו שאת רוצה.
אם זה לא עובד, אני מציע שתיצרי דוגמא קטנה ופשוטה שתאפשר לנו לשחזר את הקוד שלך במלואו (כלומר לא רק לתת פונקציות, אלא גם לתת משהו שאפשר לעבוד איתו).
פונקציה שימושית לזה היא dput.

אך על פניו, נראה שאת קרובה לאן שאת רוצה...

טל




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



2013/5/6 Ayala Allon <ayala...@gmail.com>
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-g...@googlegroups.com.

Ayala Allon

unread,
May 6, 2013, 8:20:32 AM5/6/13
to israel-r-...@googlegroups.com
טל שלום,
אני לא רוצה לחשב ממוצע אלא אחוז דיוק (רק שאני עוד לא מעבירה את זה לאחוזים).
למשל יש לי וקטור באורך של 10. הוקטור מורכב מ-0 ו-1 כאשר 1 מציין תשובה נכונה ו-0 תשובה לא נכונה. אז למשל אם צדקתי ב 8 מתוך 10 אז אני נמצאת בדיוק של 0.8 וכו'.
אז הפונקציה שכתבתי אכן עושה את זה. אפילו בדקתי אותה על data שאני כבר מכירה.
 
תודה רבה על כל התשובות
אילה
2013/5/6 Ayala Allon <ayala...@gmail.com>

To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-group+unsubscribe...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

Tal Galili

unread,
May 6, 2013, 8:22:37 AM5/6/13
to israel-r-...@googlegroups.com
.....
היי אילה,
ממוצע על וקטור של "הצליח" (1) ו"לא הצליח" (0), הוא בדיוק "פרופורציית ההצלחות".
לדוגמא:
x <- sample(c(0,1), 10, T)
mean(x)
וזה יתן אותו הדבר אם תריצי על זה את הפונקציה שלך...




2013/5/6 Ayala Allon <ayala...@gmail.com>
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-g...@googlegroups.com.

Ayala Allon

unread,
May 6, 2013, 8:25:43 AM5/6/13
to israel-r-...@googlegroups.com
תודה!


2013/5/6 Tal Galili <tal.g...@gmail.com>

--
You received this message because you are subscribed to a topic in the Google Groups "Israel R User Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/israel-r-user-group/2ZNycGAgq_k/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to israel-r-user-g...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Ayala Allon

Ayala Allon

unread,
Jun 4, 2013, 11:56:48 AM6/4/13
to israel-r-...@googlegroups.com

שלום,

 

יש לי עוד שאלה בקשר לכתיבת פונקציות.

כשאני כותבת פונקציה לפי מה שאני מבינה היא יכולה להחזיר לי רק ערך אחד (מה שאני שמה ב- return).

האם יש דרך לגרום לפונקציה להחזיר לי מספר ערכים?

למשל בפונקציה שכתבתי הייתי רוצה שהיא תחזיר לי גם את head.data וגם את dim.data.

כשאני מריצה את הפונקציה הבאה R כותב לי הודעת שגיאה שאומרת שאני לא יכולה לשים יותר מערך אחד ב-return.

read.data = function (MyFileName.csv){data = read.csv(MyFileName.csv)

                                                                                        head.data = head(data)

                                                                                        dim.data = dim(data)

                                                                                        return(haed.data,dim.data)}

 

 

הודעת השגיאה שאני מקבלת:

> read.data("r5data-exp.csv")

Error in return(haed.data, dim.data) :

  multi-argument returns are not permitted

 

תודה

אילה



2013/5/6 Ayala Allon <ayala...@gmail.com>



--
Ayala Allon

Tal Galili

unread,
Jun 4, 2013, 12:55:59 PM6/4/13
to israel-r-...@googlegroups.com
היי אילה, דרך פשוטה לעשות זאת היא להחזיר list.
לדוגמא:
fo <- function(x) return( list(first = x, poly2 = x**2, cubic = x**3, cat="miao") )
fo(5)



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



2013/6/4 Ayala Allon <ayala...@gmail.com>

Ayala Allon

unread,
Jun 4, 2013, 1:26:34 PM6/4/13
to israel-r-...@googlegroups.com

תודה!!!!

בתאריך 4 ביונ 2013 19:56, מאת "Tal Galili" <tal.g...@gmail.com>:

Yizhar Toren

unread,
Jun 4, 2013, 4:04:19 PM6/4/13
to israel-r-user-group@googlegroups com

או פשוט ווקטור עם שמות

return(c(head=head.data, dim=dim.data))

דרך אגב בצורה הזו לפונקציות כמו sapply קל מאוד להפוך את הפלט ל data.frame

Ayala Allon

unread,
Jun 5, 2013, 2:21:21 AM6/5/13
to israel-r-...@googlegroups.com
תודה רבה!!

תודה!!!!

2013/6/4 Ayala Allon <ayala...@gmail.com>


2013/5/6 Ayala Allon <ayala...@gmail.com>
תודה!


2013/5/6 Tal Galili <tal.g...@gmail.com>

--
You received this message because you are subscribed to a topic in the Google Groups "Israel R User Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/israel-r-user-group/2ZNycGAgq_k/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to israel-r-user-group+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Ayala Allon



--
Ayala Allon

--
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-group+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to a topic in the Google Groups "Israel R User Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/israel-r-user-group/2ZNycGAgq_k/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to israel-r-user-group+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

Ayala Allon

unread,
Jun 6, 2013, 10:49:55 AM6/6/13
to israel-r-...@googlegroups.com

שלום,

אני רוצה לכתוב פונקציה שתעשה את הדבר הבא:

 

condition = paste(x1,x2, sep="_")

העניין הוא שכל פעם יהיה לי מספר שונה של X  שאני ארצה לעשות בניהם paste. השאלה שלי היא איך אני יכולה לכתוב פונקציה באופן כללי ככה שכל פעם מספר האלמנטים שהיא תקבל יהיה שונה? אני רוצה לעשות את אותן האופרציות כל פעם עם מספר אלמנטים שונה.

אשמח להסבר על איך אפשר לעשות את זה.

תודה רבה,

אילה

amit gal

unread,
Jun 6, 2013, 10:58:49 AM6/6/13
to israel-r-...@googlegroups.com
תכניסי את כל האיקסים שלך לרשימה:
xlist = list(x1,x2,x3,...)

ואז:
do.call(paste,xlist,sep="_")

הפקודה do.call מאפשרת העברת רשימה (באורך משתנה) של פרמטרים, כרשימה (מופרדת פסיקים) של פרמטרים של פונקציה.

 


2013/6/6 Ayala Allon <ayala...@gmail.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.
Reply all
Reply to author
Forward
0 new messages