this in theory simple task turned out to drive me crazy today. I'm rather new to R, but got along quite well until now. Maybe someone of you is having an easier time to solve it.
In short: How do I get the maximum values per observation out of a somehow 'mixed' character matrix similar to this one?
dummy = as.matrix(c("c(1.5,2.6,3)", "2", "1.5", "c(1.8, 2.9)"))
so that my result says (in numeric): c(3, 2, 1.5, 2.9)
The longer story:
I'm coming from a
stri_match_all_regex(somestring, regexp)
to get some numbers from a plain text. This returns me a character matrix (by definition of the stri_match_all_regex function)
let it look something similar like this after stripping out some stray characters:
dummy = as.matrix(c("c(1.5,2.6,3)","2","1.5","c(1.8,2.9)"))
You already see the complication of the strings instead of vectors in my matrix here. My desired state is to identify the maximum value of each row.
Usually nothing simpler as that, I'd e.g. run
lapply(dummy, max)
But applying numerical functions obviously won't work with these characters disguised as numericals.(until this point I did not even realize that these are all characters and not numbers as they show up without quotation marks in rStudio View(dummy) ). Turning it into numerics with
as.numeric(dummy)
makes me lose the vectors within the matrix with NAs. Not what I want. I want each "c(1.2,5)" interpreted as if it would be a 'real'/'quotation-mark-less c(1.2,5), and the numbers as numbers of course too.
I even tried to strsplit / gsub the columns but that doesn't seem fruitful either or I'm just doing it wrong.
gsub( ",|c\\(|\\)", ",", dummy)
leaves me with NAs as the , isn't properly interpreted and
as.numeric(strsplit(dummy, ",|.\\(|\\)"))
won't allow me to coerce th elist object returned to numeric
Hence the straightforward question: How do I turn a character Matrix similar to dummy into a "usable" form to apply numeric functions on both, the plain numbers and the vectors consisting of numbers?
Thanks for your help! I feel like this should be easy.. but I'm stuck with it for quite a while now.