[R] Help with functions

Alex99 loyola9988 at yahoo.com
Wed Oct 22 17:53:48 CEST 2008


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.



More information about the R-help mailing list