[R] R Macro Question

Charles C. Berry cberry at tajo.ucsd.edu
Thu Apr 30 16:55:24 CEST 2009


On Wed, 29 Apr 2009, Jenn wrote:

> Dear all,
>
> I am a new R user, and I have a question about R macro.
>
> Here is the situation, I want to do item option analysis (options include: A
> , B, C, D), below is the codes for option A analyses.
>
>
> #option A analyses
>
> optiona<-mat.or.vec(nrow(responsedata2), ncol(responsedata2))
>
> # create A matrix by recoding
> for (i in 1:ncol(responsedata2)){
>     optiona[,i]<-recode(responsedata2[,i],"'A'=1;NA=NA;else=0")
> }
>
> # compute mean for A matrix
>
> pa<-rep(0,ncol(optiona))
> for (i in 1:ncol(optiona)){
>     pa[i]<-round(mean(optiona[,i],na.rm=TRUE),2)
> }
>
> pa1<-as.data.frame(pa)
>  
> I can repeat the codes for other options such as "B", "C" and "D", but I am wondering if I can do it 
> in a macro way--subsituting "A" "a" in the codes with "B" "b", etc.
>
> In SAS it can be done easily, but I could not get it to work in R.

I suspect that 'it' can also be done easily in R.

But you have not really told the list what 'it' is.

You should provide some R code that actually works- as the R Posting Guide 
recommends. The 'code' above is not syntactically valid R, so you haven't 
really run it and neither can we.

The function 'recode' is not in base R, so you have to provide it or refer 
to the contributed package frmo which it is obtained.

I can only guess what you are trying to do, but I'd say mapply() or one of 
its relatives (lapply, tapply, ... ) is probably equal to the type of task 
you wish to perform.


HTH,

Chuck

>
> Waiting for your reply.
>
> And thank you so much in advance.
>
> Meng
>
>
>
> 	[[alternative HTML version deleted]]
>
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901



More information about the R-help mailing list