# [R] Can one write a procedure in R like for instance in Maple ?

Baronin P. Storch von sputzele at mail.ru
Mon Jan 23 18:56:36 CET 2006

Dear R-wizards!

I have been learning on my own how to use this fantastic program.. but I agree with some people that even with the manuals, the faq and so on.. when you are sitting fully alone.. progress can be ... slow... very slow indeed.. In fact sometimes, looking at the "solutions" provided by  some of you- I am just flabbergasted to the point that I couldn't figure out how to come up with them myself (sometimes I don't even understand them :-( )

But after spending around three weeks on this, and starting to get fairly obsessed with it, I decided I shall ask for help,'cause i can't figure out in the documentation where I should look for this.

In Maple when I want to automatize something boring I write a procedure..
here I am not too sure.. how to bind together a few statements- most of which are functions... sounds like it would make up a personnal macro..
I am sure I am doing things in such a primitive way that the R-specialists will wince.. but that's how it goes with beginners!

Set up.

I have 4 fairly large data base with unequal number of lines (around 1200-1500), but identical number of columns (162)-years.

for each column, I construct a data-frame with the corresponding column of DB1, then from DB2, .. DB4.

This yields a data.frame in which many data are NA- some are real NAs some others are because I have to take the max of the lines. In any case, the number of NAs of each of these 4 columns is not identical.

I extract (by sorting and creating 4 new vectors) 4 vectors of variable length
-the relevant and interesting data- to whom I wish to apply some standardized treatment: that is  normality with say Shapiro-Wilks, Levene, etc.. Kruskal-Wallis and probably other things..

I am not showing my tasks because I do not think that I want to bother the readers with this,, rather ask for general remarks ( you can provide examples of your own).

For each column I want to write the results in a table.. and append these resulta for each column.

I was fairly efficient at doing that for a particular column,

but then the simple thought of  apply this "list of tasks" 162 times.. makes me..
feel that there should be a way.... to speed up my execution.. (a loop)

However I have not been able to create a super "function" (or procedure) that could tie all these statements together in a sensible fashion.. because each time the data.frame created is generated by a function.. and somehow i still did not figure out how to write a function of functions
and then maybe a loop do for all values of dates =1:162 this function..

(all the stuff I tried failed, because I was indexing objects that were also indexed.. I am vague.. but then retracing a 3 weeks of trials and errors errors errors errors ...\infty :-) is cumbersome)

1. Could anybody  give me suggestions where to look and maybe unveil the tricks of the function of functions ..

ideally I would construct a loop executing a super function.. whose results would be dumped in a file (write.table) appending each time the result of the loop i..

but I was not able to construct that...
should I make a wiser use of these "apply, tapply, sapply" marvels? I dunno.
does something like for (i in 1:G){sapply(b(i),sw)} were b(i) is the dataframe for column i, and SW is a function (super function-procedure) make sense in R?

I see that there are some fully esoterical paragraphs on things that seem to be relevant in the manuals.. but.. esoterical.. I cannot make sense of them...  vicious circle..

Thank you in advance for any courageous that  would give me a hint..

Christina