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

Marcin Jaworski semafory at wp.pl
Fri Oct 20 21:11:45 CEST 2006


jim holtman (jholtman at gmail.com) wrote:

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

Hello,

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[ !is.na( df[ ,no_of_col<-length(vector<-c(1:(ncol
(df))))] )==TRUE , ]
no_of_variables<-ncol(not_empty_df)
no_of_cases<-nrow(not_empty_df)
var_covar<-var(not_empty_df,na.rm=T)
alfa<-(no_of_variables/(no_of_variables-1)*(1-sum(diag(var_covar))/sum
(var_covar)))
Alfa<-alfa
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



More information about the R-help mailing list