I have a matrix with 99 rows called r010, r020, ... r990 and 99 cols called c010, c020, ... c990.
Additionally I have defined variables for every row and every column, like r010. This variable contains all data within the row r010.
My Input data can contain values like "{r010, c070} + {r020, c070} == {r030, c090}"
. So here it is important to call specific CELLS of the Matrix.
I would like R to go into the Matrix at row 1 (r010) and col 7 (c070) if the Input data is like {r010, c070}.
So I need a function which detects the "," and gives the value in that cell of the Matrix (row on the left side of "," and col on the left)
This works as follows:
s <- "{r390, c010} == {r400, c010} + {r410, c010} + {r420, c010}"
pat <- "\\d+(?>\\d)\\B"
pat2 <- "\\{r..., c...\\}"
getCell=function(data,string){
y=regmatches(string,gregexpr(pat,string,perl = T))
data[do.call(rbind,lapply(y,as.numeric))]
}
pos<-regmatches(string,gregexpr(pat2,s))
unlist(pos)->pos # convert to character
getCell(table,pos)#Will give the values in (39,1),(40,1),(41,1),(42,1)
Now I Need to put the results back into the original formula s
to evaluate with eval(parse(text=s))
This can work with gsubfn, but I don't get it completely.
b <- gsubfn(pat, getCell, s); b
will lead to Error in structure(.External(.C_dotTcl, ...), class = "tclObj") :
[tcl] couldn't compile regular expression pattern: quantifier operand invalid.
Can anyone help?