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]]
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.
What you are seeing when printing depends on how you do it (and it can be confusing). Try this, type the following lines exactly (or copy and paste) and carefully study the results. In particular, compare the results of cat(s,'\n') with the results of strsplit(s, '"') . s <- "{\"Q0\":\"37\",\"Q1\":\"f\"}" s cat(s,'\n') '"' # single quote, double quote, single quote strsplit(s, '"') Hope this is helpful, Dan Daniel Nordlund, PhD Research and Data Analysis Division Services & Enterprise Support Administration Washington State Department of Social and Health Services> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Patzelt, > Edward > Sent: Tuesday, March 28, 2017 9:50 AM > To: R-help at r-project.org > Subject: [R] Splitting on metacharacters > > 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.