Re: [R] I really don't understand functions in R :-)

From: Marcin Jaworski <>
Date: Fri 20 Oct 2006 - 19:11:45 GMT

jim holtman ( wrote:

>It sounds like you want to use 'local' to create private variables:


I hope I do not disturb your discussion very much writing in this thread but the topic sounded extremely familiar for me :)

My only question is:

Is there a good book or more general - any resources that can be used as a reader-friendly introduction to programming (statistical) functions in R, regarding the fact that potential reader have no programming background at all ("at all" means that such constructs like 'for', 'while' and practical usage of them are mistery)?

Below is a rationale for the above question, probably unnecessary thus skippable.

Unfortunately, I have no experience in programming at all, but I found R very usefull for statistical analysis and graphics. There's a lot of great literature focused on how to apply statistical models in R. It would be great to customize the power of R for individual needs. It appeared to me very quickly that such task requires using constructs like "for" or "while". I am ready to learn programming to use R efficiently.

My problem is that I can prepare an expression that executes a sequence of specific commands, but I cannot figure it out how to make the commands conditional or looping to get what I really need.

For instance, the example below calculates Cronbach's alpha (actually, I found more elegant way in the www, and even package psych that includes a function bringing identical results):

alfa<-function(df) {
not_empty_df<-df[ ! df[ ,no_of_col<-length(vector<-c(1:(ncol (df))))] )==TRUE , ]
alfa<-(no_of_variables/(no_of_variables-1)*(1-sum(diag(var_covar))/sum (var_covar)))

cat("Cronbach's alpha: ");print(Alfa,digits=3);
cat("Number of items in hand: ");print(no_of_variables)
cat("Number of cases: ");print(no_of_cases)

I can imagine that one can write function that shows values of Cronbach's alpha when subsequent variables in data frame are deleted separately, that is they are back in data frame again when there is the time for neighbour of previously deleted variable to be ommited during calculation. To be true, I do not want to know how the function should look like exectly. It would be pleasure for me to write such function by myself. I guess grouping, loops and/or conditional execution (as described in "An Introduction to R") is necessary to build such function.

It's a shame of me but I cannot understand the idea of "for", "while" and "repeat" after reading few pages of Introduction. I need more readable recources for dummies. Could you help me, pleaase?

Best regards,
Marcin Jaworski mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Sat Oct 21 05:12:37 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 20 Oct 2006 - 19:30:11 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.