-1

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?

Sven
  • 83
  • 10
  • Can you share your data using `dput()` & relevant codes? See more here https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Tung Mar 08 '18 at 16:49
  • What if you replace `pat <- "\\d+(?>\\d)\\B"` with `pat <- "\\d+"`? – Wiktor Stribiżew May 27 '18 at 22:55

1 Answers1

0

In base R, we can use regmatches to extract the values that are between r or c before the last 0. Then we transform then into numeric. this automatically drops the leading zero. We then use those to extract the required values from the data

fun=function(data,x){
 y=regmatches(x,gregexpr("\\d+(?>\\d)\\B",x,perl = T))
 data[do.call(rbind,lapply(y,as.numeric))]
 }

pos=c("{r010, c070}","{r0200, c0700}","{r0990, c0990}")

fun(data,pos)#Will give you values in (1,7),(20,70),(99,99)
Onyambu
  • 67,392
  • 3
  • 24
  • 53
  • Thank you. Let's say my data Looks like this: `"{r010, c070} + {r020, c070} == {r030, c090}"` Here I need to extract the `pos` part, then replace it by the value (which works with the code above) and repeat this procedure as often as a `pos` part occurs (3 times in this example). In the next part I could easily parse the result to check if `350 + 150 == 500` for example. But I think I need to use a for Loop to get that result. Thanks for further help, I'm new to regex. S. – Sven Mar 09 '18 at 16:09
  • you would need to have: `w=scan(text=gsub("\\s*[+==]\\s*","\n",y),what="character",quiet=T,sep="\n")` then do `fun(data,w)` – Onyambu Mar 09 '18 at 16:18
  • Hello, I have edited my question above. @Onyambu this soultion doesn't work for me – Sven Mar 26 '18 at 12:46
  • Before you edited it was working. You did not have to edit it. You could have just asked a new question. This question was asked more than 2weeks ago. Even on editting no one will answer it. There was no need of unaccepting the answer just because it is not working in current data. But since you have the power to do so. Its okay. – Onyambu Mar 26 '18 at 15:38