[R] Pesky homemade function code

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Thu Sep 16 09:46:25 CEST 2010


The calculation of semivraiogram is allready implemented is many R
packages. So why do it yourself?

library("sos")
findFn("variogram")

Gives you 190 hits!

------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium

Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium

tel. + 32 54/436 185
Thierry.Onkelinx op inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
data.
~ John Tukey
  

> -----Oorspronkelijk bericht-----
> Van: r-help-bounces op r-project.org 
> [mailto:r-help-bounces op r-project.org] Namens trekvana
> Verzonden: donderdag 16 september 2010 8:35
> Aan: r-help op r-project.org
> Onderwerp: [R] Pesky homemade function code
> 
> 
> Hi all-
> 
> this seems to be simple to figure out but since im new to 
> writing functions I dont know what is happening. Here is my 
> code along with the error I am
> receiving:
> 
> semivario=function(data,ids,times,resids){
> id=unique(data$ids)
> index=combinations(length(data$times[data$ids==id[1]]),2)
> time=gamma=numeric(dim(index)[1])
> for (j in 1:dim(index)[1]){
> 
> time[j]=abs(data$times[data$ids==id[1]][index[j,1]]-data$times
> [data$ids==id[1]][index[j,2]])
> 
> gamma[j]=.5*(data$resids[data$ids==id[1]][index[j,1]]-data$res
> ids[data$ids==id[1]][index[j,2]])^2
> }
> 
> for (i in id[-1]){
> 	index=combinations(length(data$times[data$ids==i]),2)
> 	time1=gamma1=numeric(dim(index)[1])
> 	for (j in 1:dim(index)[1]){
> 	
> time1[j]=abs(data$times[data$ids==i][index[j,1]]-data$times[da
> ta$ids==i][index[j,2]])
> 	
> gamma1[j]=.5*(data$resids[data$ids==i][index[j,1]]-data$resids
> [data$ids==i][index[j,2]])^2
> 	}
> time=c(time,time1)
> gamma=c(gamma,gamma1)
> }
> value=list(time=time,gamma=gamma)
> return(value)
> }
> 
> and I would run it as
> semivario(data=hope,ids=id,times=sctime,resids=scResid)
> 
> What I want to do is everywhere we see the words
> data,ids,times,resids to be replaced with, for example,
> hope,id,sctime,scResid. Think of macros in SAS where we use 
> the & before every macro input variable name. Im not sure if 
> this is how R handles function inputs.
> 
> Here is my error:
> Error in combinations(length(data$times[data$ids == id[1]]), 2) : 
>   bad value of n
> 
> 
> Thanks
> 
> --
> View this message in context: 
> http://r.789695.n4.nabble.com/Pesky-homemade-function-code-tp2
> 541597p2541597.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help op 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.
> 

Druk dit bericht a.u.b. niet onnodig af.
Please do not print this message unnecessarily.

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer 
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in  this message 
and any annex are purely those of the writer and may not be regarded as stating 
an official position of INBO, as long as the message is not confirmed by a duly 
signed document.



More information about the R-help mailing list