I have the following code.
completemodel <- function(model, colnum)
{
modlst = c()
tuplenum = length(model)
if(tuplenum != 0)
for(i in 1:tuplenum)
modlst = c(modlst, model[[i]])
index = seq(0, colnum-1)
inddiff = setdiff(index, modlst)
inddifflen = length(inddiff)
for(i in seq(length.out=inddifflen))
model = append(model, inddiff[i])
return(model)
}
## Calculate number of parameters in model.
numparam <- function(mod, colnum)
{
library(RJSONIO)
mod = fromJSON(mod)
mod = completemodel(mod, colnum)
totnum = 0
for(tup in mod)
totnum = totnum +(4**length(tup))
return(totnum)
}
x = cbind.data.frame(rownum=c(100, 100), colnum=c(10, 20), modeltrue=c("[]", "[]"), modelresult=c("[[1,2]]","[[1,3]]"), stringsAsFactors=FALSE)
> x
rows colnum modeltrue modelresult
1 100 10 [] [[1,2]]
2 100 20 [] [[1,3]]
How can I operate on x to give me a data frame that looks like the following? Here of course
I mean that the value of e.g. numparam("[]", 10) when I write numparam("[]", 10).
rownum colnum numparam_modeltrue numparam_modelresult
100 10 numparam("[]", 10) numparam("[[1,2]]", 10)
100 20 numparam("[]", 20) numparam("[[1,3]]", 20)
Some version of the apply function might work, but I am having problems finding the proper formulation.
UPDATE: It seems that if the rownnum, colnum tuple is not unique, then one can do the following.
x = cbind.data.frame(id=c(1, 2, 3), rownum=c(100, 100, 100), colnum=c(10, 20, 20), modeltrue=c("[]", "[]", "[]"),
modelresult=c("[[1,2]]","[[1,3]]","[[1,3, 4]]"), stringsAsFactors = FALSE)
##Then, create a data.table and set the key
library(data.table)
xDT <- as.data.table(x)
setkeyv(xDT, c("id", "rownum", "colnum")
Is that the correct method?
numparamfunction to obtain another data frame in the manner speciried. What is unclear? This is the actual code I am using. I suppose I could come up with a simpler example to illustrate, though this one is not very complex.numparam_modeltrueandnumparam_modelresultare factors?cbind.