[R] Splitting on metacharacters

Boris Steipe boris.steipe at utoronto.ca
Tue Mar 28 19:48:27 CEST 2017


You haven't actually _defined_ what the expected range of variations of your string are, but if I speculate wildly, I can come up with the following two solutions that may get you started.

s <- '*"{\"Q0\":\"37\",\"Q1\":\"f\"}"*'
regmatches(s, gregexpr("([0-9]{2,}|[a-z]+)", s))[[1]]
strsplit(s, "\\\"")[[1]][c(5, 9)]


Obviously, there are many more possibilities.
B.


> On Mar 28, 2017, at 12:50 PM, Patzelt, Edward <patzelt at g.harvard.edu> wrote:
> 
> Hi R-help,
> 
> I would like to:
> 
> Extract the "26" and "f" from the following string (i.e. age and gender).
> I've tried a bunch of strsplit, grep, etc. Please help!
> 
> *"{\"Q0\":\"37\",\"Q1\":\"f\"}"*
> 
> 
> Thanks,
> 
> Edward
> 
> -- 
> Edward H Patzelt | Clinical Science PhD Student
> Psychology | Harvard University
> *Computational Cognitive Neuroscience Laboratory
> <http://gershmanlab.webfactional.com/>*
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list