[R] Pesky homemade function code
trekvana
trekvana at aol.com
Thu Sep 16 08:35:22 CEST 2010
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$resids[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[data$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-tp2541597p2541597.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list