[R] Help with functions

jim holtman jholtman at gmail.com
Wed Oct 22 22:44:52 CEST 2008


It might be your return statement.  Look up 'return' in the help file
and you will see that you can only return a single value.  If you want
multiple values returned, use a list:

return(listGmean,Gsd))

On Wed, Oct 22, 2008 at 11:53 AM, Alex99 <loyola9988 at yahoo.com> wrote:
>
> Hi there,
>
> I have a the following function which takes a  name(fname), total number of
> rows (Total), number of times to make a sample(nSample), number of
> records/rows to be included in each sample(nPatient). then it generates
> required samples and calculates the mean and standard deviation of the
> columns for each sample and finally in calculates the grand mean and
> standard deviation.
>
> The problem is if I run each line of the code separately (not in a function)
> it works but when I put it in a function it doesn't!!!
>
> i.e. if I delete the first line, and put "Mydata"(where ever I have fname)
> and 900(where ever I have Total) and 5 (where ever I have nSample) and 100
> (where ever I have nPatient) and run each line separately it works fine. But
> when I make a function like the one below and call it like:
> sampling(Mydata,900,5,100)
> I get this error message:
> "Error in dimnames(x) <- dn : length of 'dimnames' [2] not equal to array
> extent"
>
> I used "debug" on the sampling function it seems the error comes from line
> 4:
> "result=replicate(nSample,trans[,sample(colnames(trans),nPatient)],simplify=FALSE)"
>
> any idea why do I get this error and how to fix it?
> Thanks,
>
> sampling=function(fname,Total,nSample,nPatient){
> trans=t(A)
> colnames(trans) <- c(1:Total)
> result=replicate(nSample,trans[,sample(colnames(trans),nPatient)],simplify=FALSE)
> show(result)
>
> Means=do.call(rbind,lapply(result,function(x) rowMeans(x)))
> rownames(Means)=paste('sample',1:nSample,sep="")
> show(Means)
> Gmean=colMeans(Means)
> Gmean
>
> # load genefilter package
> STDs=do.call(rbind,lapply(result,function(x) rowSds(x)))
> rownames(STDs)=paste('sample',1:nSample,sep="")
> show(STDs)
> Gsd=sd(STDs)
> Gsd
>
> return(Gmean,Gsd)}
> --
> View this message in context: http://www.nabble.com/Help-with-functions-tp20113920p20113920.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list