Re: [R] string concatenation

2020-11-05 Thread Jiefei Wang
Hi,

Thanks for clarifying, there is no quote in the result. The quote in the
output is just R's way to tell you that the variable got printed is a
string. If you add quotes around a string, it will get printed like

"\"Alice, Bob, Charles\""

I hope this helps.

Best,
Jiefei

On Thu, Nov 5, 2020 at 6:51 PM Duncan Murdoch 
wrote:

> On 05/11/2020 3:20 a.m., Edjabou Vincent wrote:
> > Following John request, I am wondering if it is possible to get this
> result:
> >
> > Alice, Bob, Charles (without bracket )
>
> I think you mean "without the quotes".  Use noquote():
>
>noquote(paste0(x,collapse = ", "))
>
> will print without quotes.  (Internally the data is the same, but a
> class attribute is added that tells print() not to add quotes.)
>
> Duncan Murdoch
> >
> > Thank you for your help
> >
> >
> >
> > Med venlig hilsen/ Best regards
> >
> > Vincent Edjabou
> > Mobile: +45 31 95 99 33
> > linkedin.com/vincent
> > 
> >
> > Orcid: -0003-2849-6151
> >
> >
> >
> > On Thu, Nov 5, 2020 at 6:27 AM Jiefei Wang  wrote:
> >
> >> Hi John,
> >>
> >> Try paste0(x,collapse = ", ")
> >>
> >> Best,
> >> Jiefei
> >>
> >> On Thu, Nov 5, 2020 at 1:16 PM John  wrote:
> >>
> >>> Hi,
> >>>
> >>> I have a sequence of characters:
> >>>
> >>> x <- c("Alice", "Bob", "Charles")
> >>>
> >>> How can I produce the following results:
> >>>
> >>> "Alice, Bob, Charles"
> >>>
> >>> ?
> >>>
> >>> paste? merge?
> >>>
> >>> Thank you very much!
> >>>
> >>>  [[alternative HTML version deleted]]
> >>>
> >>> __
> >>> R-help@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.
> >>>
> >>
> >>  [[alternative HTML version deleted]]
> >>
> >> __
> >> R-help@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.
> >>
> >
> >   [[alternative HTML version deleted]]
> >
> > __
> > R-help@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.
> >
>
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] string concatenation

2020-11-05 Thread Duncan Murdoch

On 05/11/2020 3:20 a.m., Edjabou Vincent wrote:

Following John request, I am wondering if it is possible to get this result:

Alice, Bob, Charles (without bracket )


I think you mean "without the quotes".  Use noquote():

  noquote(paste0(x,collapse = ", "))

will print without quotes.  (Internally the data is the same, but a 
class attribute is added that tells print() not to add quotes.)


Duncan Murdoch


Thank you for your help



Med venlig hilsen/ Best regards

Vincent Edjabou
Mobile: +45 31 95 99 33
linkedin.com/vincent


Orcid: -0003-2849-6151



On Thu, Nov 5, 2020 at 6:27 AM Jiefei Wang  wrote:


Hi John,

Try paste0(x,collapse = ", ")

Best,
Jiefei

On Thu, Nov 5, 2020 at 1:16 PM John  wrote:


Hi,

I have a sequence of characters:

x <- c("Alice", "Bob", "Charles")

How can I produce the following results:

"Alice, Bob, Charles"

?

paste? merge?

Thank you very much!

 [[alternative HTML version deleted]]

__
R-help@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.



 [[alternative HTML version deleted]]

__
R-help@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.



[[alternative HTML version deleted]]

__
R-help@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.



__
R-help@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.


Re: [R] string concatenation

2020-11-05 Thread Edjabou Vincent
Following John request, I am wondering if it is possible to get this result:

Alice, Bob, Charles (without bracket )

Thank you for your help



Med venlig hilsen/ Best regards

Vincent Edjabou
Mobile: +45 31 95 99 33
linkedin.com/vincent


Orcid: -0003-2849-6151



On Thu, Nov 5, 2020 at 6:27 AM Jiefei Wang  wrote:

> Hi John,
>
> Try paste0(x,collapse = ", ")
>
> Best,
> Jiefei
>
> On Thu, Nov 5, 2020 at 1:16 PM John  wrote:
>
> > Hi,
> >
> > I have a sequence of characters:
> >
> > x <- c("Alice", "Bob", "Charles")
> >
> > How can I produce the following results:
> >
> > "Alice, Bob, Charles"
> >
> > ?
> >
> > paste? merge?
> >
> > Thank you very much!
> >
> > [[alternative HTML version deleted]]
> >
> > __
> > R-help@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.
> >
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] string concatenation

2020-11-04 Thread Jiefei Wang
Hi John,

Try paste0(x,collapse = ", ")

Best,
Jiefei

On Thu, Nov 5, 2020 at 1:16 PM John  wrote:

> Hi,
>
> I have a sequence of characters:
>
> x <- c("Alice", "Bob", "Charles")
>
> How can I produce the following results:
>
> "Alice, Bob, Charles"
>
> ?
>
> paste? merge?
>
> Thank you very much!
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


[R] string concatenation

2020-11-04 Thread John
Hi,

I have a sequence of characters:

x <- c("Alice", "Bob", "Charles")

How can I produce the following results:

"Alice, Bob, Charles"

?

paste? merge?

Thank you very much!

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] String replace

2019-04-03 Thread Ivan Krylov
On Wed, 3 Apr 2019 15:01:37 +0100
Graham Leask via R-help  wrote:

Suppose that `BHC$Date` contains a string "M_24".

You do:

> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_2" ,
> "01-04-2017"))

before you have a chance to do:

> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date ,
> "M_24" , "01-02-2019"))

So now it has "01-04-20174" because the first expression had a
successful match and already made a replacement.

help() for stringi::stringi-search-regex says that look-around
expressions are supported, so one of the ways to prevent this would be
to modify your patterns to look like e.g. 'M_2(?!\\d)' to match
'M_2' that is *not* followed by a digit.

-- 
Best regards,
Ivan

__
R-help@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.


Re: [R] String replace

2019-04-03 Thread Sarah Goslee
Try reversing the order, or extending the to-replace bit.

"M_1" is finding and replacing "M_10", "M_11", etc.
"M_2" is finding and replacing "M_20", "M_21", etc.

So M_14 becomes "01-03-20174"

In the future, a toy dataset would help with the reproducible example
aspect, and make your question easier to answer.

Sarah

On Wed, Apr 3, 2019 at 1:24 PM Graham Leask via R-help
 wrote:
>
>
> I’m attempting to replace a string variable that normally works fine. However 
> when trying to
> do this with a string in the form of a date the output becomes corrupted.
>
> See below:
>
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_1" , 
> "01-03-2017"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_2" , 
> "01-04-2017"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_3" , 
> "01-05-2017"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_4" , 
> "01-06-2017"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_5" , 
> "01-07-2017"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_6" , 
> "01-08-2017"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_7" , 
> "01-09-2017"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_8" , 
> "01-10-2017"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_9" , 
> "01-11-2017"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_10" , 
> "01-12-2017"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_11" , 
> "01-01-2018"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_12" , 
> "01-02-2018"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_13" , 
> "01-03-2018"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_14" , 
> "01-04-2018"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_15" , 
> "01-05-2018"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_16" , 
> "01-06-2018"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_17" , 
> "01-07-2018"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_18" , 
> "01-08-2018"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_19" , 
> "01-09-2018"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_20" , 
> "01-10-2018"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_21" , 
> "01-11-2018"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_22" , 
> "01-12-2018"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_23" , 
> "01-01-2019"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_24" , 
> "01-02-2019"))
>
> The first line works fine but by the end the output is corrupted to
>
> "01-03-20174"
>
>
> Any thoughts what I’m doing wrong here?
>
> __
> R-help@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.



-- 
Sarah Goslee (she/her)
http://www.numberwright.com

__
R-help@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.


[R] String replace

2019-04-03 Thread Graham Leask via R-help


I’m attempting to replace a string variable that normally works fine. However 
when trying to
do this with a string in the form of a date the output becomes corrupted.

See below:

BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_1" , 
"01-03-2017"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_2" , 
"01-04-2017"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_3" , 
"01-05-2017"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_4" , 
"01-06-2017"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_5" , 
"01-07-2017"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_6" , 
"01-08-2017"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_7" , 
"01-09-2017"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_8" , 
"01-10-2017"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_9" , 
"01-11-2017"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_10" , 
"01-12-2017"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_11" , 
"01-01-2018"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_12" , 
"01-02-2018"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_13" , 
"01-03-2018"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_14" , 
"01-04-2018"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_15" , 
"01-05-2018"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_16" , 
"01-06-2018"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_17" , 
"01-07-2018"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_18" , 
"01-08-2018"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_19" , 
"01-09-2018"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_20" , 
"01-10-2018"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_21" , 
"01-11-2018"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_22" , 
"01-12-2018"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_23" , 
"01-01-2019"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_24" , 
"01-02-2019"))

The first line works fine but by the end the output is corrupted to

"01-03-20174"


Any thoughts what I’m doing wrong here?

__
R-help@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.


Re: [R] string pattern matching

2017-03-23 Thread Joe Ceradini
Oh, I see the utility of that now. Definitely will be using "term.labels"
(which I was not aware of). Thanks!

On Thu, Mar 23, 2017 at 9:48 AM, William Dunlap  wrote:

> If you are trying to see if one model nested in another then I think
> looking at the 'term.labels' attribute of terms(formula) is the way to
> go.  Most formula-based modelling functions store the output of
> terms(formula) in their output and many supply a method for the terms
> function that extracts that from their output.
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
>
> On Thu, Mar 23, 2017 at 6:37 AM, Joe Ceradini 
> wrote:
> > Thanks for the additional response, Bill. I did not want to bog down
> > the question with the full context of the function. Briefly, given a
> > set of nested and non-nested regression models, I want to compare AIC
> > (bigger model - smaller model) and do an LRT for all the nested models
> > that differ by a single predictor. All models, nested or not, would
> > also have an AIC value (I am aware of the critiques of mixing p-value
> > hypothesis testing and information criteria). So, not quite
> > MuMIn::dredge. The tricky part, for me, has been doing the comparisons
> > for only the nested models in a set that contains nested and
> > non-nested. I made some progress with the function, so I'll refrain
> > from bugging the list with the whole thing unless (when) I'm stuck
> > again.
> >
> > For those interested in the motivation, I'm running with the idea of
> > trying to flag uninformative parameters which "steal" AIC model
> > weight, and potentially result in a misleading model set, depending
> > how the reader interprets the set.
> > Arnold, T. W. 2010. Uninformative parameters and model selection using
> > Akaike’s information criterion. Journal of Wildlife Management
> > 74:1175–1178.
> > Murtaugh, P. 2014. In defense of P values. Ecology 95:611–617.
> >
> > Joe
> >
> > On Wed, Mar 22, 2017 at 9:11 AM, William Dunlap 
> wrote:
> >> You did not describe the goal of your pattern matching.  Were you trying
> >> to match any string that could be interpreted as an R expression
> containing
> >> X1 and X3 as additive terms?   If so, you could turn the string into a
> one-sided
> >> formula and use the terms() function.  E.g.,
> >>
> >> f <- function(string) {
> >> fmla <- as.formula(paste("~", string))
> >> term.labels <- attr(terms(fmla), "term.labels")
> >> all(c("X1","X3") %in% term.labels)
> >> }
> >>
> >>> f("X3 + X2 + X1")
> >> [1] TRUE
> >>> f("- X3 + X2 + X1")
> >> [1] FALSE
> >>> f("X3 + X2 + log(X1)")
> >> [1] FALSE
> >>> f("X3 + X2 + log(X1) + X1")
> >> [1] TRUE
> >> Bill Dunlap
> >> TIBCO Software
> >> wdunlap tibco.com
> >>
> >>
> >> On Wed, Mar 22, 2017 at 6:39 AM, Joe Ceradini 
> wrote:
> >>> Wow. Thanks to everyone (Jim, Ng Bo Lin, Bert, David, and Ulrik) for
> >>> all the quick and helpful responses. They have given me a better
> >>> understanding of regular expressions, and certainly answered my
> >>> question.
> >>>
> >>> Joe
> >>>
> >>> On Wed, Mar 22, 2017 at 12:22 AM, Ulrik Stervbo <
> ulrik.ster...@gmail.com> wrote:
>  Hi Joe,
> 
>  you could also rethink your pattern:
> 
>  grep("x1 \\+ x2", test, value = TRUE)
> 
>  grep("x1 \\+ x", test, value = TRUE)
> 
>  grep("x1 \\+ x[0-9]", test, value = TRUE)
> 
>  HTH
>  Ulrik
> 
>  On Wed, 22 Mar 2017 at 02:10 Jim Lemon  wrote:
> >
> > Hi Joe,
> > This may help you:
> >
> > test <- c("x1", "x2", "x3", "x1 + x2 + x3")
> > multigrep<-function(x1,x2) {
> >  xbits<-unlist(strsplit(x1," "))
> >  nbits<-length(xbits)
> >  xans<-rep(FALSE,nbits)
> >  for(i in 1:nbits) if(length(grep(xbits[i],x2))) xans[i]<-TRUE
> >  return(all(xans))
> > }
> > multigrep("x1 + x3","x1 + x2 + x3")
> > [1] TRUE
> > multigrep("x1 + x4","x1 + x2 + x3")
> > [1] FALSE
> >
> > Jim
> >
> > On Wed, Mar 22, 2017 at 10:50 AM, Joe Ceradini <
> joecerad...@gmail.com>
> > wrote:
> > > Hi Folks,
> > >
> > > Is there a way to find "x1 + x2 + x3" given "x1 + x3" as the
> pattern?
> > > Or is that a ridiculous question, since I'm trying to find
> something
> > > based on a pattern that doesn't exist?
> > >
> > > test <- c("x1", "x2", "x3", "x1 + x2 + x3")
> > > test
> > > [1] "x1"   "x2"   "x3"   "x1 + x2 + x3"
> > >
> > > grep("x1 + x2", test, fixed=TRUE, value = TRUE)
> > > [1] "x1 + x2 + x3"
> > >
> > >
> > > But what if only have "x1 + x3" as the pattern and still want to
> > > return "x1 + x2 + x3"?
> > >
> > > grep("x1 + x3", test, fixed=TRUE, value = TRUE)
> > > character(0)
> > >
> > > I'm sure this looks like an odd question. I'm trying to build a
> > > function and stuck on this. Rather than dropping 

Re: [R] string pattern matching

2017-03-23 Thread William Dunlap via R-help
If you are trying to see if one model nested in another then I think
looking at the 'term.labels' attribute of terms(formula) is the way to
go.  Most formula-based modelling functions store the output of
terms(formula) in their output and many supply a method for the terms
function that extracts that from their output.
Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Thu, Mar 23, 2017 at 6:37 AM, Joe Ceradini  wrote:
> Thanks for the additional response, Bill. I did not want to bog down
> the question with the full context of the function. Briefly, given a
> set of nested and non-nested regression models, I want to compare AIC
> (bigger model - smaller model) and do an LRT for all the nested models
> that differ by a single predictor. All models, nested or not, would
> also have an AIC value (I am aware of the critiques of mixing p-value
> hypothesis testing and information criteria). So, not quite
> MuMIn::dredge. The tricky part, for me, has been doing the comparisons
> for only the nested models in a set that contains nested and
> non-nested. I made some progress with the function, so I'll refrain
> from bugging the list with the whole thing unless (when) I'm stuck
> again.
>
> For those interested in the motivation, I'm running with the idea of
> trying to flag uninformative parameters which "steal" AIC model
> weight, and potentially result in a misleading model set, depending
> how the reader interprets the set.
> Arnold, T. W. 2010. Uninformative parameters and model selection using
> Akaike’s information criterion. Journal of Wildlife Management
> 74:1175–1178.
> Murtaugh, P. 2014. In defense of P values. Ecology 95:611–617.
>
> Joe
>
> On Wed, Mar 22, 2017 at 9:11 AM, William Dunlap  wrote:
>> You did not describe the goal of your pattern matching.  Were you trying
>> to match any string that could be interpreted as an R expression containing
>> X1 and X3 as additive terms?   If so, you could turn the string into a 
>> one-sided
>> formula and use the terms() function.  E.g.,
>>
>> f <- function(string) {
>> fmla <- as.formula(paste("~", string))
>> term.labels <- attr(terms(fmla), "term.labels")
>> all(c("X1","X3") %in% term.labels)
>> }
>>
>>> f("X3 + X2 + X1")
>> [1] TRUE
>>> f("- X3 + X2 + X1")
>> [1] FALSE
>>> f("X3 + X2 + log(X1)")
>> [1] FALSE
>>> f("X3 + X2 + log(X1) + X1")
>> [1] TRUE
>> Bill Dunlap
>> TIBCO Software
>> wdunlap tibco.com
>>
>>
>> On Wed, Mar 22, 2017 at 6:39 AM, Joe Ceradini  wrote:
>>> Wow. Thanks to everyone (Jim, Ng Bo Lin, Bert, David, and Ulrik) for
>>> all the quick and helpful responses. They have given me a better
>>> understanding of regular expressions, and certainly answered my
>>> question.
>>>
>>> Joe
>>>
>>> On Wed, Mar 22, 2017 at 12:22 AM, Ulrik Stervbo  
>>> wrote:
 Hi Joe,

 you could also rethink your pattern:

 grep("x1 \\+ x2", test, value = TRUE)

 grep("x1 \\+ x", test, value = TRUE)

 grep("x1 \\+ x[0-9]", test, value = TRUE)

 HTH
 Ulrik

 On Wed, 22 Mar 2017 at 02:10 Jim Lemon  wrote:
>
> Hi Joe,
> This may help you:
>
> test <- c("x1", "x2", "x3", "x1 + x2 + x3")
> multigrep<-function(x1,x2) {
>  xbits<-unlist(strsplit(x1," "))
>  nbits<-length(xbits)
>  xans<-rep(FALSE,nbits)
>  for(i in 1:nbits) if(length(grep(xbits[i],x2))) xans[i]<-TRUE
>  return(all(xans))
> }
> multigrep("x1 + x3","x1 + x2 + x3")
> [1] TRUE
> multigrep("x1 + x4","x1 + x2 + x3")
> [1] FALSE
>
> Jim
>
> On Wed, Mar 22, 2017 at 10:50 AM, Joe Ceradini 
> wrote:
> > Hi Folks,
> >
> > Is there a way to find "x1 + x2 + x3" given "x1 + x3" as the pattern?
> > Or is that a ridiculous question, since I'm trying to find something
> > based on a pattern that doesn't exist?
> >
> > test <- c("x1", "x2", "x3", "x1 + x2 + x3")
> > test
> > [1] "x1"   "x2"   "x3"   "x1 + x2 + x3"
> >
> > grep("x1 + x2", test, fixed=TRUE, value = TRUE)
> > [1] "x1 + x2 + x3"
> >
> >
> > But what if only have "x1 + x3" as the pattern and still want to
> > return "x1 + x2 + x3"?
> >
> > grep("x1 + x3", test, fixed=TRUE, value = TRUE)
> > character(0)
> >
> > I'm sure this looks like an odd question. I'm trying to build a
> > function and stuck on this. Rather than dropping the whole function on
> > the list, I thought I'd try one piece I needed help with...although I
> > suspect that this question itself probably does bode well for my
> > function :)
> >
> > Thanks!
> > Joe
> >
> > __
> > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the 

Re: [R] string pattern matching

2017-03-23 Thread Joe Ceradini
Thanks for the additional response, Bill. I did not want to bog down
the question with the full context of the function. Briefly, given a
set of nested and non-nested regression models, I want to compare AIC
(bigger model - smaller model) and do an LRT for all the nested models
that differ by a single predictor. All models, nested or not, would
also have an AIC value (I am aware of the critiques of mixing p-value
hypothesis testing and information criteria). So, not quite
MuMIn::dredge. The tricky part, for me, has been doing the comparisons
for only the nested models in a set that contains nested and
non-nested. I made some progress with the function, so I'll refrain
from bugging the list with the whole thing unless (when) I'm stuck
again.

For those interested in the motivation, I'm running with the idea of
trying to flag uninformative parameters which "steal" AIC model
weight, and potentially result in a misleading model set, depending
how the reader interprets the set.
Arnold, T. W. 2010. Uninformative parameters and model selection using
Akaike’s information criterion. Journal of Wildlife Management
74:1175–1178.
Murtaugh, P. 2014. In defense of P values. Ecology 95:611–617.

Joe

On Wed, Mar 22, 2017 at 9:11 AM, William Dunlap  wrote:
> You did not describe the goal of your pattern matching.  Were you trying
> to match any string that could be interpreted as an R expression containing
> X1 and X3 as additive terms?   If so, you could turn the string into a 
> one-sided
> formula and use the terms() function.  E.g.,
>
> f <- function(string) {
> fmla <- as.formula(paste("~", string))
> term.labels <- attr(terms(fmla), "term.labels")
> all(c("X1","X3") %in% term.labels)
> }
>
>> f("X3 + X2 + X1")
> [1] TRUE
>> f("- X3 + X2 + X1")
> [1] FALSE
>> f("X3 + X2 + log(X1)")
> [1] FALSE
>> f("X3 + X2 + log(X1) + X1")
> [1] TRUE
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
>
> On Wed, Mar 22, 2017 at 6:39 AM, Joe Ceradini  wrote:
>> Wow. Thanks to everyone (Jim, Ng Bo Lin, Bert, David, and Ulrik) for
>> all the quick and helpful responses. They have given me a better
>> understanding of regular expressions, and certainly answered my
>> question.
>>
>> Joe
>>
>> On Wed, Mar 22, 2017 at 12:22 AM, Ulrik Stervbo  
>> wrote:
>>> Hi Joe,
>>>
>>> you could also rethink your pattern:
>>>
>>> grep("x1 \\+ x2", test, value = TRUE)
>>>
>>> grep("x1 \\+ x", test, value = TRUE)
>>>
>>> grep("x1 \\+ x[0-9]", test, value = TRUE)
>>>
>>> HTH
>>> Ulrik
>>>
>>> On Wed, 22 Mar 2017 at 02:10 Jim Lemon  wrote:

 Hi Joe,
 This may help you:

 test <- c("x1", "x2", "x3", "x1 + x2 + x3")
 multigrep<-function(x1,x2) {
  xbits<-unlist(strsplit(x1," "))
  nbits<-length(xbits)
  xans<-rep(FALSE,nbits)
  for(i in 1:nbits) if(length(grep(xbits[i],x2))) xans[i]<-TRUE
  return(all(xans))
 }
 multigrep("x1 + x3","x1 + x2 + x3")
 [1] TRUE
 multigrep("x1 + x4","x1 + x2 + x3")
 [1] FALSE

 Jim

 On Wed, Mar 22, 2017 at 10:50 AM, Joe Ceradini 
 wrote:
 > Hi Folks,
 >
 > Is there a way to find "x1 + x2 + x3" given "x1 + x3" as the pattern?
 > Or is that a ridiculous question, since I'm trying to find something
 > based on a pattern that doesn't exist?
 >
 > test <- c("x1", "x2", "x3", "x1 + x2 + x3")
 > test
 > [1] "x1"   "x2"   "x3"   "x1 + x2 + x3"
 >
 > grep("x1 + x2", test, fixed=TRUE, value = TRUE)
 > [1] "x1 + x2 + x3"
 >
 >
 > But what if only have "x1 + x3" as the pattern and still want to
 > return "x1 + x2 + x3"?
 >
 > grep("x1 + x3", test, fixed=TRUE, value = TRUE)
 > character(0)
 >
 > I'm sure this looks like an odd question. I'm trying to build a
 > function and stuck on this. Rather than dropping the whole function on
 > the list, I thought I'd try one piece I needed help with...although I
 > suspect that this question itself probably does bode well for my
 > function :)
 >
 > Thanks!
 > Joe
 >
 > __
 > R-help@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.

 __
 R-help@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.
>>
>>
>>
>> --
>> Cooperative Fish and Wildlife Research Unit
>> Zoology and Physiology Dept.
>> University of Wyoming
>> 

Re: [R] string pattern matching

2017-03-22 Thread William Dunlap via R-help
You did not describe the goal of your pattern matching.  Were you trying
to match any string that could be interpreted as an R expression containing
X1 and X3 as additive terms?   If so, you could turn the string into a one-sided
formula and use the terms() function.  E.g.,

f <- function(string) {
fmla <- as.formula(paste("~", string))
term.labels <- attr(terms(fmla), "term.labels")
all(c("X1","X3") %in% term.labels)
}

> f("X3 + X2 + X1")
[1] TRUE
> f("- X3 + X2 + X1")
[1] FALSE
> f("X3 + X2 + log(X1)")
[1] FALSE
> f("X3 + X2 + log(X1) + X1")
[1] TRUE
Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Wed, Mar 22, 2017 at 6:39 AM, Joe Ceradini  wrote:
> Wow. Thanks to everyone (Jim, Ng Bo Lin, Bert, David, and Ulrik) for
> all the quick and helpful responses. They have given me a better
> understanding of regular expressions, and certainly answered my
> question.
>
> Joe
>
> On Wed, Mar 22, 2017 at 12:22 AM, Ulrik Stervbo  
> wrote:
>> Hi Joe,
>>
>> you could also rethink your pattern:
>>
>> grep("x1 \\+ x2", test, value = TRUE)
>>
>> grep("x1 \\+ x", test, value = TRUE)
>>
>> grep("x1 \\+ x[0-9]", test, value = TRUE)
>>
>> HTH
>> Ulrik
>>
>> On Wed, 22 Mar 2017 at 02:10 Jim Lemon  wrote:
>>>
>>> Hi Joe,
>>> This may help you:
>>>
>>> test <- c("x1", "x2", "x3", "x1 + x2 + x3")
>>> multigrep<-function(x1,x2) {
>>>  xbits<-unlist(strsplit(x1," "))
>>>  nbits<-length(xbits)
>>>  xans<-rep(FALSE,nbits)
>>>  for(i in 1:nbits) if(length(grep(xbits[i],x2))) xans[i]<-TRUE
>>>  return(all(xans))
>>> }
>>> multigrep("x1 + x3","x1 + x2 + x3")
>>> [1] TRUE
>>> multigrep("x1 + x4","x1 + x2 + x3")
>>> [1] FALSE
>>>
>>> Jim
>>>
>>> On Wed, Mar 22, 2017 at 10:50 AM, Joe Ceradini 
>>> wrote:
>>> > Hi Folks,
>>> >
>>> > Is there a way to find "x1 + x2 + x3" given "x1 + x3" as the pattern?
>>> > Or is that a ridiculous question, since I'm trying to find something
>>> > based on a pattern that doesn't exist?
>>> >
>>> > test <- c("x1", "x2", "x3", "x1 + x2 + x3")
>>> > test
>>> > [1] "x1"   "x2"   "x3"   "x1 + x2 + x3"
>>> >
>>> > grep("x1 + x2", test, fixed=TRUE, value = TRUE)
>>> > [1] "x1 + x2 + x3"
>>> >
>>> >
>>> > But what if only have "x1 + x3" as the pattern and still want to
>>> > return "x1 + x2 + x3"?
>>> >
>>> > grep("x1 + x3", test, fixed=TRUE, value = TRUE)
>>> > character(0)
>>> >
>>> > I'm sure this looks like an odd question. I'm trying to build a
>>> > function and stuck on this. Rather than dropping the whole function on
>>> > the list, I thought I'd try one piece I needed help with...although I
>>> > suspect that this question itself probably does bode well for my
>>> > function :)
>>> >
>>> > Thanks!
>>> > Joe
>>> >
>>> > __
>>> > R-help@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.
>>>
>>> __
>>> R-help@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.
>
>
>
> --
> Cooperative Fish and Wildlife Research Unit
> Zoology and Physiology Dept.
> University of Wyoming
> joecerad...@gmail.com / 914.707.8506
> wyocoopunit.org
>
> __
> R-help@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.

__
R-help@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.


Re: [R] string pattern matching

2017-03-22 Thread Joe Ceradini
Wow. Thanks to everyone (Jim, Ng Bo Lin, Bert, David, and Ulrik) for
all the quick and helpful responses. They have given me a better
understanding of regular expressions, and certainly answered my
question.

Joe

On Wed, Mar 22, 2017 at 12:22 AM, Ulrik Stervbo  wrote:
> Hi Joe,
>
> you could also rethink your pattern:
>
> grep("x1 \\+ x2", test, value = TRUE)
>
> grep("x1 \\+ x", test, value = TRUE)
>
> grep("x1 \\+ x[0-9]", test, value = TRUE)
>
> HTH
> Ulrik
>
> On Wed, 22 Mar 2017 at 02:10 Jim Lemon  wrote:
>>
>> Hi Joe,
>> This may help you:
>>
>> test <- c("x1", "x2", "x3", "x1 + x2 + x3")
>> multigrep<-function(x1,x2) {
>>  xbits<-unlist(strsplit(x1," "))
>>  nbits<-length(xbits)
>>  xans<-rep(FALSE,nbits)
>>  for(i in 1:nbits) if(length(grep(xbits[i],x2))) xans[i]<-TRUE
>>  return(all(xans))
>> }
>> multigrep("x1 + x3","x1 + x2 + x3")
>> [1] TRUE
>> multigrep("x1 + x4","x1 + x2 + x3")
>> [1] FALSE
>>
>> Jim
>>
>> On Wed, Mar 22, 2017 at 10:50 AM, Joe Ceradini 
>> wrote:
>> > Hi Folks,
>> >
>> > Is there a way to find "x1 + x2 + x3" given "x1 + x3" as the pattern?
>> > Or is that a ridiculous question, since I'm trying to find something
>> > based on a pattern that doesn't exist?
>> >
>> > test <- c("x1", "x2", "x3", "x1 + x2 + x3")
>> > test
>> > [1] "x1"   "x2"   "x3"   "x1 + x2 + x3"
>> >
>> > grep("x1 + x2", test, fixed=TRUE, value = TRUE)
>> > [1] "x1 + x2 + x3"
>> >
>> >
>> > But what if only have "x1 + x3" as the pattern and still want to
>> > return "x1 + x2 + x3"?
>> >
>> > grep("x1 + x3", test, fixed=TRUE, value = TRUE)
>> > character(0)
>> >
>> > I'm sure this looks like an odd question. I'm trying to build a
>> > function and stuck on this. Rather than dropping the whole function on
>> > the list, I thought I'd try one piece I needed help with...although I
>> > suspect that this question itself probably does bode well for my
>> > function :)
>> >
>> > Thanks!
>> > Joe
>> >
>> > __
>> > R-help@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.
>>
>> __
>> R-help@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.



-- 
Cooperative Fish and Wildlife Research Unit
Zoology and Physiology Dept.
University of Wyoming
joecerad...@gmail.com / 914.707.8506
wyocoopunit.org

__
R-help@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.


Re: [R] string pattern matching

2017-03-22 Thread Ulrik Stervbo
Hi Joe,

you could also rethink your pattern:

grep("x1 \\+ x2", test, value = TRUE)

grep("x1 \\+ x", test, value = TRUE)

grep("x1 \\+ x[0-9]", test, value = TRUE)

HTH
Ulrik

On Wed, 22 Mar 2017 at 02:10 Jim Lemon  wrote:

> Hi Joe,
> This may help you:
>
> test <- c("x1", "x2", "x3", "x1 + x2 + x3")
> multigrep<-function(x1,x2) {
>  xbits<-unlist(strsplit(x1," "))
>  nbits<-length(xbits)
>  xans<-rep(FALSE,nbits)
>  for(i in 1:nbits) if(length(grep(xbits[i],x2))) xans[i]<-TRUE
>  return(all(xans))
> }
> multigrep("x1 + x3","x1 + x2 + x3")
> [1] TRUE
> multigrep("x1 + x4","x1 + x2 + x3")
> [1] FALSE
>
> Jim
>
> On Wed, Mar 22, 2017 at 10:50 AM, Joe Ceradini 
> wrote:
> > Hi Folks,
> >
> > Is there a way to find "x1 + x2 + x3" given "x1 + x3" as the pattern?
> > Or is that a ridiculous question, since I'm trying to find something
> > based on a pattern that doesn't exist?
> >
> > test <- c("x1", "x2", "x3", "x1 + x2 + x3")
> > test
> > [1] "x1"   "x2"   "x3"   "x1 + x2 + x3"
> >
> > grep("x1 + x2", test, fixed=TRUE, value = TRUE)
> > [1] "x1 + x2 + x3"
> >
> >
> > But what if only have "x1 + x3" as the pattern and still want to
> > return "x1 + x2 + x3"?
> >
> > grep("x1 + x3", test, fixed=TRUE, value = TRUE)
> > character(0)
> >
> > I'm sure this looks like an odd question. I'm trying to build a
> > function and stuck on this. Rather than dropping the whole function on
> > the list, I thought I'd try one piece I needed help with...although I
> > suspect that this question itself probably does bode well for my
> > function :)
> >
> > Thanks!
> > Joe
> >
> > __
> > R-help@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.
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] string pattern matching

2017-03-21 Thread David Winsemius

> On Mar 21, 2017, at 6:31 PM, Bert Gunter  wrote:
> 
> It is not clear to me what you mean, but:
> 
>> grep ("x1 \\+.* \\+ x3",test, value = TRUE)
> [1] "x1 + x2 + x3"
> 
> ## This will miss "x1 + x3" though.

So then this might be acceptable:

grep ("x1\\ \\+.* x3", test, value = TRUE)


> 
> seems to do what you want, maybe. Perhaps you need to read up about
> regular expressions and/or clarify what you want to do.
> 
> 
> Cheers,
> Bert
> Bert Gunter
> 
> "The trouble with having an open mind is that people keep coming along
> and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> 
> 
> On Tue, Mar 21, 2017 at 4:50 PM, Joe Ceradini  wrote:
>> Hi Folks,
>> 
>> Is there a way to find "x1 + x2 + x3" given "x1 + x3" as the pattern?
>> Or is that a ridiculous question, since I'm trying to find something
>> based on a pattern that doesn't exist?
>> 
>> test <- c("x1", "x2", "x3", "x1 + x2 + x3")
>> test
>> [1] "x1"   "x2"   "x3"   "x1 + x2 + x3"
>> 
>> grep("x1 + x2", test, fixed=TRUE, value = TRUE)
>> [1] "x1 + x2 + x3"
>> 
>> 
>> But what if only have "x1 + x3" as the pattern and still want to
>> return "x1 + x2 + x3"?
>> 
>> grep("x1 + x3", test, fixed=TRUE, value = TRUE)
>> character(0)
>> 
>> I'm sure this looks like an odd question. I'm trying to build a
>> function and stuck on this. Rather than dropping the whole function on
>> the list, I thought I'd try one piece I needed help with...although I
>> suspect that this question itself probably does bode well for my
>> function :)
>> 
>> Thanks!
>> Joe
>> 
>> __
>> R-help@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.
> 
> __
> R-help@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.

David Winsemius
Alameda, CA, USA

__
R-help@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.


Re: [R] string pattern matching

2017-03-21 Thread Bert Gunter
It is not clear to me what you mean, but:

> grep ("x1 \\+.* \\+ x3",test, value = TRUE)
[1] "x1 + x2 + x3"

## This will miss "x1 + x3" though.

seems to do what you want, maybe. Perhaps you need to read up about
regular expressions and/or clarify what you want to do.


Cheers,
Bert
Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Tue, Mar 21, 2017 at 4:50 PM, Joe Ceradini  wrote:
> Hi Folks,
>
> Is there a way to find "x1 + x2 + x3" given "x1 + x3" as the pattern?
> Or is that a ridiculous question, since I'm trying to find something
> based on a pattern that doesn't exist?
>
> test <- c("x1", "x2", "x3", "x1 + x2 + x3")
> test
> [1] "x1"   "x2"   "x3"   "x1 + x2 + x3"
>
> grep("x1 + x2", test, fixed=TRUE, value = TRUE)
> [1] "x1 + x2 + x3"
>
>
> But what if only have "x1 + x3" as the pattern and still want to
> return "x1 + x2 + x3"?
>
> grep("x1 + x3", test, fixed=TRUE, value = TRUE)
> character(0)
>
> I'm sure this looks like an odd question. I'm trying to build a
> function and stuck on this. Rather than dropping the whole function on
> the list, I thought I'd try one piece I needed help with...although I
> suspect that this question itself probably does bode well for my
> function :)
>
> Thanks!
> Joe
>
> __
> R-help@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.

__
R-help@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.


Re: [R] string pattern matching

2017-03-21 Thread Jim Lemon
Hi Joe,
This may help you:

test <- c("x1", "x2", "x3", "x1 + x2 + x3")
multigrep<-function(x1,x2) {
 xbits<-unlist(strsplit(x1," "))
 nbits<-length(xbits)
 xans<-rep(FALSE,nbits)
 for(i in 1:nbits) if(length(grep(xbits[i],x2))) xans[i]<-TRUE
 return(all(xans))
}
multigrep("x1 + x3","x1 + x2 + x3")
[1] TRUE
multigrep("x1 + x4","x1 + x2 + x3")
[1] FALSE

Jim

On Wed, Mar 22, 2017 at 10:50 AM, Joe Ceradini  wrote:
> Hi Folks,
>
> Is there a way to find "x1 + x2 + x3" given "x1 + x3" as the pattern?
> Or is that a ridiculous question, since I'm trying to find something
> based on a pattern that doesn't exist?
>
> test <- c("x1", "x2", "x3", "x1 + x2 + x3")
> test
> [1] "x1"   "x2"   "x3"   "x1 + x2 + x3"
>
> grep("x1 + x2", test, fixed=TRUE, value = TRUE)
> [1] "x1 + x2 + x3"
>
>
> But what if only have "x1 + x3" as the pattern and still want to
> return "x1 + x2 + x3"?
>
> grep("x1 + x3", test, fixed=TRUE, value = TRUE)
> character(0)
>
> I'm sure this looks like an odd question. I'm trying to build a
> function and stuck on this. Rather than dropping the whole function on
> the list, I thought I'd try one piece I needed help with...although I
> suspect that this question itself probably does bode well for my
> function :)
>
> Thanks!
> Joe
>
> __
> R-help@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.

__
R-help@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.


[R] string pattern matching

2017-03-21 Thread Joe Ceradini
Hi Folks,

Is there a way to find "x1 + x2 + x3" given "x1 + x3" as the pattern?
Or is that a ridiculous question, since I'm trying to find something
based on a pattern that doesn't exist?

test <- c("x1", "x2", "x3", "x1 + x2 + x3")
test
[1] "x1"   "x2"   "x3"   "x1 + x2 + x3"

grep("x1 + x2", test, fixed=TRUE, value = TRUE)
[1] "x1 + x2 + x3"


But what if only have "x1 + x3" as the pattern and still want to
return "x1 + x2 + x3"?

grep("x1 + x3", test, fixed=TRUE, value = TRUE)
character(0)

I'm sure this looks like an odd question. I'm trying to build a
function and stuck on this. Rather than dropping the whole function on
the list, I thought I'd try one piece I needed help with...although I
suspect that this question itself probably does bode well for my
function :)

Thanks!
Joe

__
R-help@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.


Re: [R] String match

2016-06-04 Thread Bert Gunter
Please (re)-read the Help file for pmatch, which says:

... " (A partial match occurs if the whole of the element of x matches
the beginning of the element of table.) "

This is clearly not your situation. One approach (there may be others
depending on what your detailed situation actually is) is to use
regular expressions:

?regexp   ?regexpr

Cheers,
Bert
Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sat, Jun 4, 2016 at 10:28 AM, Christofer Bogaso
 wrote:
> Hi again,
>
> I am facing trouble to match a vector strings. Let say I have below
> full string vector
>
> WhereToLook = c("ultracemco.bo.openam"   ,  "ultracemco.bo.higham"
> ,"ultracemco.bo.lowam"   ,   "ultracemco.bo.closeam"   ,
> "ultracemco.bo.volumeam"   ,"ultracemco.bo.adjustedam")
>
> WhatToLook = c("volume", "close")
>
> Basically I need to find the positions of WhatToLook in WhereToLook.
> So my code goes as below :
>
>> pmatch(tolower(colnames(WhereToLook)), WhatToLook)
> integer(0)
>
>
> Although I was expecting my code would point 5 & 4 respectively.
>
> Appreciate if someone points as correct code for above case.
>
> Thanks,
>
> __
> R-help@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.

__
R-help@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.


Re: [R] String match

2016-06-04 Thread jim holtman
try this:

> WhereToLook = c("ultracemco.bo.openam"   ,  "ultracemco.bo.higham"
+  ,"ultracemco.bo.lowam"   ,   "ultracemco.bo.closeam"   ,
+  "ultracemco.bo.volumeam"   ,"ultracemco.bo.adjustedam")
>
>  WhatToLook = c("volume", "close")
>
> # create pattern match
> pat <- paste(WhatToLook, collapse = "|")
> grep(pat, WhereToLook)
[1] 4 5




Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

On Sat, Jun 4, 2016 at 1:28 PM, Christofer Bogaso <
bogaso.christo...@gmail.com> wrote:

> Hi again,
>
> I am facing trouble to match a vector strings. Let say I have below
> full string vector
>
> WhereToLook = c("ultracemco.bo.openam"   ,  "ultracemco.bo.higham"
> ,"ultracemco.bo.lowam"   ,   "ultracemco.bo.closeam"   ,
> "ultracemco.bo.volumeam"   ,"ultracemco.bo.adjustedam")
>
> WhatToLook = c("volume", "close")
>
> Basically I need to find the positions of WhatToLook in WhereToLook.
> So my code goes as below :
>
> > pmatch(tolower(colnames(WhereToLook)), WhatToLook)
> integer(0)
>
>
> Although I was expecting my code would point 5 & 4 respectively.
>
> Appreciate if someone points as correct code for above case.
>
> Thanks,
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


[R] String match

2016-06-04 Thread Christofer Bogaso
Hi again,

I am facing trouble to match a vector strings. Let say I have below
full string vector

WhereToLook = c("ultracemco.bo.openam"   ,  "ultracemco.bo.higham"
,"ultracemco.bo.lowam"   ,   "ultracemco.bo.closeam"   ,
"ultracemco.bo.volumeam"   ,"ultracemco.bo.adjustedam")

WhatToLook = c("volume", "close")

Basically I need to find the positions of WhatToLook in WhereToLook.
So my code goes as below :

> pmatch(tolower(colnames(WhereToLook)), WhatToLook)
integer(0)


Although I was expecting my code would point 5 & 4 respectively.

Appreciate if someone points as correct code for above case.

Thanks,

__
R-help@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.


Re: [R] String Matching

2016-02-01 Thread Berend Hasselman

> On 1 Feb 2016, at 08:03, PIKAL Petr <petr.pi...@precheza.cz> wrote:
> 
> Hi
> 
> Maybe I am completely wrong but do you really need regular expressions?
> 
> You say you want to compare first nine characters of id?
> 
>> substr(id, 1,9)==cusip
> [1] TRUE
>> 
> 
> or the last six?
> 
>> substr(id, nchar(id)-6, nchar(id))=="432.rds"
> [1] TRUE
>> 
> 
> Cheers
> Petr
> 
> 
>> -Original Message-
>> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Glenn
>> Schultz
>> Sent: Friday, January 29, 2016 6:02 PM
>> To: R Help R
>> Subject: [R] String Matching
>> 
>> All,
>> 
>> I have a file named as so 313929BL4FNMA2432.rds  the user may pass
>> either the first 9 character or the last six characters.  I need to
>> match the remainder of the file name using either the first nine or
>> last six.  I have read the help files for Regular Expression as used in
>> R and I think what I want to use is glob2rx.
>> 
>> I have worked a minimal example to test my code:
>> 
>> id <- "313929BL4FNMA2432.rds"
>> cusip <- "313929BL4"
>> poolnumm <- "FNMA2432"
>> paste(cusip, ".*", ".rds")
>> glob2rx(paste(cusip, ".*", ".rds"), trim.head = TRUE, trim.tail = TRUE)
>> 
>> This returns false which leads me to believe that it is not working
>> glob2rx(paste(cusip, ".*", ".rds"), trim.head = TRUE, trim.tail = TRUE)
>> == id
>> 
>> I am going to use as follows in the function below - which returns the
>> error file not found
>> 
>> MBS_Test <- function(MBS.id = "character"){ MBS <-
>> glob2rx(paste(MBS.id, ".*", "//.rds", sep = ""), trim.tail = TRUE)
>> MBS.Conn <- gzfile(description = paste(system.file(package =
>> "BondLab"), "/BondData/", MBS, sep = ""), open = "rb") MBS <-
>> readRDS(MBS.Conn)
>> on.exit(close.connection(MBS.Conn))
>> return(MBS)
>> }
>> 


I don't think you are using (glob) wild characters correctly; where you write 
.* you likely need *?
In addition why not use paste0, which does not use  as separator,  
instead of paste?
Finally your poolnumm variable consists of 8 characters and not 6.

If you change your minimal example to this:

paste0(cusip, "*", ".rds")
glob2rx(paste0(cusip, "*", ".rds"))
grepl(glob2rx(paste0(cusip, "*", ".rds")), id)
grepl(glob2rx(paste0("*", poolnumm, ".rds")), id)

you get TRUE twice.

But Petr's solution for the first 9 characters is much simpler.
And for matching the last 6 (8) you'll have to remove the extension first and 
then use substr (if I understand your problem correctly).

Berend

__
R-help@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.


Re: [R] String Matching

2016-02-01 Thread PIKAL Petr
Hi

Maybe I am completely wrong but do you really need regular expressions?

You say you want to compare first nine characters of id?

> substr(id, 1,9)==cusip
[1] TRUE
>

or the last six?

> substr(id, nchar(id)-6, nchar(id))=="432.rds"
[1] TRUE
>

Cheers
Petr


> -Original Message-
> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Glenn
> Schultz
> Sent: Friday, January 29, 2016 6:02 PM
> To: R Help R
> Subject: [R] String Matching
>
> All,
>
> I have a file named as so 313929BL4FNMA2432.rds  the user may pass
> either the first 9 character or the last six characters.  I need to
> match the remainder of the file name using either the first nine or
> last six.  I have read the help files for Regular Expression as used in
> R and I think what I want to use is glob2rx.
>
> I have worked a minimal example to test my code:
>
> id <- "313929BL4FNMA2432.rds"
> cusip <- "313929BL4"
> poolnumm <- "FNMA2432"
> paste(cusip, ".*", ".rds")
> glob2rx(paste(cusip, ".*", ".rds"), trim.head = TRUE, trim.tail = TRUE)
>
> This returns false which leads me to believe that it is not working
> glob2rx(paste(cusip, ".*", ".rds"), trim.head = TRUE, trim.tail = TRUE)
> == id
>
> I am going to use as follows in the function below - which returns the
> error file not found
>
> MBS_Test <- function(MBS.id = "character"){ MBS <-
> glob2rx(paste(MBS.id, ".*", "//.rds", sep = ""), trim.tail = TRUE)
> MBS.Conn <- gzfile(description = paste(system.file(package =
> "BondLab"), "/BondData/", MBS, sep = ""), open = "rb") MBS <-
> readRDS(MBS.Conn)
> on.exit(close.connection(MBS.Conn))
> return(MBS)
> }
>
> Any help to get me in the right direction is appreciated - Glenn
> __
> R-help@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.


Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny 
pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně 
jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze 
svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email 
jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či 
zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a 
to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; 
Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce 
s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným 
dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost 
žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně 
pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně 
osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi 
či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are 
intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. 
Delete the contents of this e-mail with all attachments and its copies from 
your system.
If you are not the intended recipient of this e-mail, you are not authorized to 
use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by 
modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a 
contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately 
accept such offer; The sender of this e-mail (offer) excludes any acceptance of 
the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an 
express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter i

[R] String Matching

2016-01-29 Thread Glenn Schultz

All,

I have a file named as so 313929BL4FNMA2432.rds  the user may pass either the 
first 9 character or the last six characters.  I need to match the remainder of 
the file name using either the first nine or last six.  I have read the help 
files for Regular Expression as used in R and I think what I want to use is 
glob2rx.  

I have worked a minimal example to test my code:

id <- "313929BL4FNMA2432.rds" 
cusip <- "313929BL4"

poolnumm <- "FNMA2432"
paste(cusip, ".*", ".rds")
glob2rx(paste(cusip, ".*", ".rds"), trim.head = TRUE, trim.tail = TRUE)

This returns false which leads me to believe that it is not working
glob2rx(paste(cusip, ".*", ".rds"), trim.head = TRUE, trim.tail = TRUE) == id

I am going to use as follows in the function below - which returns the error 
file not found

MBS_Test <- function(MBS.id = "character"){
MBS <- glob2rx(paste(MBS.id, ".*", "//.rds", sep = ""), trim.tail = TRUE)
MBS.Conn <- gzfile(description = paste(system.file(package = "BondLab"),
"/BondData/", MBS, sep = ""), open = "rb") 
MBS <- readRDS(MBS.Conn)

on.exit(close.connection(MBS.Conn))
return(MBS)
}   

Any help to get me in the right direction is appreciated - Glenn
__
R-help@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.

Re: [R] String Matching

2016-01-29 Thread Duncan Murdoch

On 29/01/2016 12:02 PM, Glenn Schultz wrote:

All,

I have a file named as so 313929BL4FNMA2432.rds  the user may pass either the 
first 9 character or the last six characters.  I need to match the remainder of 
the file name using either the first nine or last six.  I have read the help 
files for Regular Expression as used in R and I think what I want to use is 
glob2rx.

I have worked a minimal example to test my code:

id <- "313929BL4FNMA2432.rds"
cusip <- "313929BL4"
poolnumm <- "FNMA2432"
paste(cusip, ".*", ".rds")
glob2rx(paste(cusip, ".*", ".rds"), trim.head = TRUE, trim.tail = TRUE)

This returns false which leads me to believe that it is not working


No, it returns a regular expression.  You need to tell us what you 
really did if you want help fixing it.


Duncan Murdoch


glob2rx(paste(cusip, ".*", ".rds"), trim.head = TRUE, trim.tail = TRUE) == id

I am going to use as follows in the function below - which returns the error 
file not found

MBS_Test <- function(MBS.id = "character"){
MBS <- glob2rx(paste(MBS.id, ".*", "//.rds", sep = ""), trim.tail = TRUE)
MBS.Conn <- gzfile(description = paste(system.file(package = "BondLab"),
"/BondData/", MBS, sep = ""), open = "rb")
MBS <- readRDS(MBS.Conn)
on.exit(close.connection(MBS.Conn))
return(MBS)
}

Any help to get me in the right direction is appreciated - Glenn
__
R-help@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.



__
R-help@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.


Re: [R] string split problem

2015-10-23 Thread Jun Shen
Hi Marc/Bill
Thanks for reply. That's exactly what I am looking for.
Jun

On Fri, Oct 23, 2015 at 3:53 PM, William Dunlap  wrote:

> > test <- c('aaa.bb.cc','aaa.dd', 'aaa', 'aaa.', '.eee', '')
> > sub("([^.]*)(.*)", "\\1", test)
> [1] "aaa" "aaa" "aaa" "aaa" """"
> > sub("([^.]*)(.*)", "\\2", test)
> [1] ".bb.cc" ".dd"""   "."  ".eee"   ""
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
>
> On Fri, Oct 23, 2015 at 12:17 PM, Jun Shen  wrote:
> > Dear list,
> >
> > Say I have a vector that has two different types of string
> >
> > test <- c('aaa.bb.cc','aaa.dd')
> >
> > I want to extract the first part of the string (aaa) as a name and save
> the
> > rest of the string as another name.
> >
> > I was thinking something like
> >
> > sub('(.*)\\.(.*)','\\1',test) but doesn't give me what I want.
> >
> >
> > Appreciate any comments. Thanks.
> >
> > Jun
> >
> > [[alternative HTML version deleted]]
> >
> > __
> > R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


[R] string split problem

2015-10-23 Thread Jun Shen
Dear list,

Say I have a vector that has two different types of string

test <- c('aaa.bb.cc','aaa.dd')

I want to extract the first part of the string (aaa) as a name and save the
rest of the string as another name.

I was thinking something like

sub('(.*)\\.(.*)','\\1',test) but doesn't give me what I want.


Appreciate any comments. Thanks.

Jun

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] string split problem

2015-10-23 Thread William Dunlap
> test <- c('aaa.bb.cc','aaa.dd', 'aaa', 'aaa.', '.eee', '')
> sub("([^.]*)(.*)", "\\1", test)
[1] "aaa" "aaa" "aaa" "aaa" """"
> sub("([^.]*)(.*)", "\\2", test)
[1] ".bb.cc" ".dd"""   "."  ".eee"   ""
Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Fri, Oct 23, 2015 at 12:17 PM, Jun Shen  wrote:
> Dear list,
>
> Say I have a vector that has two different types of string
>
> test <- c('aaa.bb.cc','aaa.dd')
>
> I want to extract the first part of the string (aaa) as a name and save the
> rest of the string as another name.
>
> I was thinking something like
>
> sub('(.*)\\.(.*)','\\1',test) but doesn't give me what I want.
>
>
> Appreciate any comments. Thanks.
>
> Jun
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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.

__
R-help@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.


Re: [R] string split problem

2015-10-23 Thread Marc Schwartz

> On Oct 23, 2015, at 2:17 PM, Jun Shen  wrote:
> 
> Dear list,
> 
> Say I have a vector that has two different types of string
> 
> test <- c('aaa.bb.cc','aaa.dd')
> 
> I want to extract the first part of the string (aaa) as a name and save the
> rest of the string as another name.
> 
> I was thinking something like
> 
> sub('(.*)\\.(.*)','\\1',test) but doesn't give me what I want.
> 
> 
> Appreciate any comments. Thanks.
> 
> Jun


How about something like this, which presumes that the characters (besides the 
periods) are only letters:

> gsub("^([[:alpha:]]+)\\.(.*)$", "\\1|\\2", test) 
[1] "aaa|bb.cc" "aaa|dd"   

or

> sub("^([[:alpha:]]+)\\.(.*)$", "\\1|\\2", test) 
[1] "aaa|bb.cc" "aaa|dd"   


The above takes the two components, before and after the first '.', adds the 
"|" as a character in between, to then be used in strsplit():


> strsplit(gsub("^([[:alpha:]]+)\\.(.*)$", "\\1|\\2", test), split = "\\|") 
[[1]]
[1] "aaa"   "bb.cc"

[[2]]
[1] "aaa" "dd" 


See ?regex

Regards,

Marc Schwartz

__
R-help@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.


Re: [R] String Matching

2015-08-13 Thread MacQueen, Don
I haven't tested this, but what about:


df - data.frame(mtch=c(matchString, string1, string2))

grep(searchString, df$mtch, ignore.case=FALSE)

Depending on what your next step is, you might prefer grepl.


Sometimes using fixed=TRUE in grep() helps.

-Don

-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 8/12/15, 8:51 AM, R-help on behalf of Kevin Kowitski
r-help-boun...@r-project.org on behalf of k.kowit...@icloud.com wrote:

df-as.data.frame(c(matchString, string1, string2))
df
 c(matchString,
string1, string2)
1 09:11:57.259 - Assay File Processing Thread - INFO -
SolenoidCycleMessage: Addr = 0x03
2 
23:12:43.22 - Test
3 
   test
grep(searchString, df, ignore.case=FALSE)
i

__
R-help@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.


[R] String Matching

2015-08-12 Thread Kevin Kowitski

Hey everyone, 

  I have been having an issue trying to find a specific string of text in a log 
of system messages.  I have tried to use pmatch, match, and some regular 
expressions but all to no avail.  

I have a matrix / data.frame (either one, the file outputs a tens of thousands 
of rows with a single column) of strings in the following format with different 
items after INFO:
 09:11:57.259 - Assay File Processing Thread - INFO - SolenoidCycleMessage: Addr = 
0x03 

as an example I would like to match SolenoidCycleMessage
searchString-SolenoidCycleMessage
matchString-09:11:57.259 - Assay File Processing Thread - INFO - 
SolenoidCycleMessage: Addr = 0x03


pmatch(searchString, matchString)

[1] NA


match(searchString, matchString)

[1] NA

match(matchString, searchString)

[1] NA

grep(searchString, matchString, ignore.case=FALSE)

[1] 1

df-as.data.frame(c(matchString, string1, string2))
df

                                                         c(matchString, 
string1, string2)
1 09:11:57.259 - Assay File Processing Thread - INFO - SolenoidCycleMessage: 
Addr = 0x03 
2                                                                      
23:12:43.22 - Test
3                                                                               
     test

grep(searchString, df, ignore.case=FALSE)

integer(0)


grep(searchString, c(matchString, string1, string2), ignore.case=FALSE)

[1] 1

Doe anyone have some input that could help?

Thanks, 
Kevin
__
R-help@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.

Re: [R] String Matching

2015-08-12 Thread Ista Zahn
Hi Kevin,

It's not totally clear to me what the desired output is.

grep(searchString, matchString, ignore.case=FALSE)

told you that searchString is in the first element of matchString.
Isn't that what you want to know? If not, perhaps you can be more
specific about what the desired result is.

Best,
Ista

On Wed, Aug 12, 2015 at 11:51 AM, Kevin Kowitski k.kowit...@icloud.com wrote:
 Hey everyone,

   I have been having an issue trying to find a specific string of text in a
 log of system messages.  I have tried to use pmatch, match, and some regular
 expressions but all to no avail.

 I have a matrix / data.frame (either one, the file outputs a tens of
 thousands of rows with a single column) of strings in the following format
 with different items after INFO:
  09:11:57.259 - Assay File Processing Thread - INFO - SolenoidCycleMessage:
 Addr = 0x03 

 as an example I would like to match SolenoidCycleMessage
 searchString-SolenoidCycleMessage
 matchString-09:11:57.259 - Assay File Processing Thread - INFO -
 SolenoidCycleMessage: Addr = 0x03

 pmatch(searchString, matchString)

 [1] NA

 match(searchString, matchString)

 [1] NA

 match(matchString, searchString)

 [1] NA

 grep(searchString, matchString, ignore.case=FALSE)

 [1] 1

 df-as.data.frame(c(matchString, string1, string2))
 df

  c(matchString,
 string1, string2)
 1 09:11:57.259 - Assay File Processing Thread - INFO - SolenoidCycleMessage:
 Addr = 0x03
 2
 23:12:43.22 - Test
 3
 test

 grep(searchString, df, ignore.case=FALSE)

 integer(0)

 grep(searchString, c(matchString, string1, string2), ignore.case=FALSE)

 [1] 1

 Doe anyone have some input that could help?

 Thanks,
 Kevin
 __
 R-help@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.

__
R-help@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.


[R] String manipulation

2014-12-08 Thread Gang Chen
I want to do the following: if a string does not contain a colon (:),
no change is needed; if it contains one or more colons, break the
string into multiple strings using the colon as a separator. For
example, happy: becomes

happy :

:sad turns to

: sad

and happy:sad changes to

happy : sad

How to do this?

Thanks,
Gang

__
R-help@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.


Re: [R] String manipulation

2014-12-08 Thread William Dunlap
strsplit(split=:) does almost what you want, but it omits the colons from
the output.  You can use perl zero-length look-ahead and look-behind
operators in the split argument to get the colons as well:

 strsplit(c(:sad, happy:, happy:sad), split=(?=:)|(?=:),
perl=TRUE)
[[1]]
[1] :   sad

[[2]]
[1] happy :

[[3]]
[1] happy : sad



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Mon, Dec 8, 2014 at 9:08 AM, Gang Chen gangch...@gmail.com wrote:

 I want to do the following: if a string does not contain a colon (:),
 no change is needed; if it contains one or more colons, break the
 string into multiple strings using the colon as a separator. For
 example, happy: becomes

 happy :

 :sad turns to

 : sad

 and happy:sad changes to

 happy : sad

 How to do this?

 Thanks,
 Gang

 __
 R-help@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.


[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] String manipulation

2014-12-08 Thread William Dunlap
Actually, the zero-length look-ahead expression is enough to get the job
done:

 strsplit(c(:sad, happy:, happy:sad, :happy:sad:subdued:),
split=(?=:), perl=TRUE)
[[1]]
[1] :   sad

[[2]]
[1] happy :

[[3]]
[1] happy : sad

[[4]]
[1] :   happy   :   sad :   subdued :



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Mon, Dec 8, 2014 at 1:13 PM, William Dunlap wdun...@tibco.com wrote:

 strsplit(split=:) does almost what you want, but it omits the colons
 from the output.  You can use perl zero-length look-ahead and look-behind
 operators in the split argument to get the colons as well:

  strsplit(c(:sad, happy:, happy:sad), split=(?=:)|(?=:),
 perl=TRUE)
 [[1]]
 [1] :   sad

 [[2]]
 [1] happy :

 [[3]]
 [1] happy : sad



 Bill Dunlap
 TIBCO Software
 wdunlap tibco.com

 On Mon, Dec 8, 2014 at 9:08 AM, Gang Chen gangch...@gmail.com wrote:

 I want to do the following: if a string does not contain a colon (:),
 no change is needed; if it contains one or more colons, break the
 string into multiple strings using the colon as a separator. For
 example, happy: becomes

 happy :

 :sad turns to

 : sad

 and happy:sad changes to

 happy : sad

 How to do this?

 Thanks,
 Gang

 __
 R-help@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.




[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] String comparison, trailing blanks make a difference.

2014-07-19 Thread John McKown
On Fri, Jul 18, 2014 at 11:17 AM, John McKown
john.archie.mck...@gmail.com wrote:
 Well, this was a shock to me. And I don't really see any documentation
 about it, but perhaps I just can't see it.

abc == abc 
 [1] FALSE

 I guess that I thought of strings in R like I do is some other
 languages where the shorter value is padded with blanks to the length
 of the longer value, then compared. I.e. that trailing blanks didn't
 matter.

 The best solution that I have found is to use the str_trim() function
 from the stringr to remove all the trailing blanks after I get the
 data from the SQL data base. I cannot change the SQL schema to make
 the column a varchar instead of a char column. It is a vendor DB. And
 I don't know an ANSI SQL standard way to remove trailing blanks in the
 SELECT command. PostgreSQL has a trim(trailing ' ' from column)', but
 MS-SQL upchucks on that syntax.


Well, here I am - talking to myself ... again.

My problem was, of course, of my own making. I am getting my data
via RODBC from MS-SQL Server. I was basically doing a SELECT * FROM
TABLE. I normally use PostgreSQL, not MS-SQL, and I tend to use the
TEXT data type instead of CHAR or VARCHAR. So when I do the SELECT,
I get back my data without trailing blanks. Well, the data I am
reading now is created by a software vendor. I guess in order to be
database independent, the vendor designed his tables to have only
fixed length CHAR, and INT values in it. The fixed length CHAR values
are, naturally, padded on the right with blanks. Of course, now that I
understand this (weird as it is to me), I know to use a SELECT which
specifically lists the columns that I want _and_ does a TRIM() on them
to remove trailing blanks. This will reduce the size, in bytes, in my
data.frame and make it easier to use the comparison operators. Given
how the vendor saves the data, I am quite surprised that they didn't
use SQLite. The tables are simple. There are no stored procedures,
no VIEWs, no use of SCHEMAs to make subsets. Basically they just want
a simple data store, with the ability to do _simple_ joins. SQLite
seems, to me, to be a better fit than requiring the user to have a
full blown RDMS such as MS-SQL or Oracle.

Well, thanks for the whack on the head to wake me up and make me
really look at my data.

-- 
There is nothing more pleasant than traveling and meeting new people!
Genghis Khan

Maranatha! 
John McKown

__
R-help@r-project.org mailing list
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.


Re: [R] String comparison, trailing blanks make a difference.

2014-07-19 Thread Hadley Wickham
If you have unicode strings, you may need to do even more because
there are often multiple ways of representing the same glyph. I made a
little demo at http://rpubs.com/hadley/unicode-normalisation, since
any unicode characters are likely to get mangled by email.

Hadley

On Fri, Jul 18, 2014 at 11:32 AM, William Dunlap wdun...@tibco.com wrote:
abc == abc 
 [1] FALSE

 R does no interpretation of strings when doing comparisons so you do
 have do your own canonicalization.  That may involve removing
 trailing, leading, or all white space or punctuation, converting to
 lower or upper case, mapping nicknames to official names, trimming to
 a fixed number of characters, etc.

 Bill Dunlap
 TIBCO Software
 wdunlap tibco.com


 On Fri, Jul 18, 2014 at 9:17 AM, John McKown
 john.archie.mck...@gmail.com wrote:
 Well, this was a shock to me. And I don't really see any documentation
 about it, but perhaps I just can't see it.

abc == abc 
 [1] FALSE

 I guess that I thought of strings in R like I do is some other
 languages where the shorter value is padded with blanks to the length
 of the longer value, then compared. I.e. that trailing blanks didn't
 matter.

 The best solution that I have found is to use the str_trim() function
 from the stringr to remove all the trailing blanks after I get the
 data from the SQL data base. I cannot change the SQL schema to make
 the column a varchar instead of a char column. It is a vendor DB. And
 I don't know an ANSI SQL standard way to remove trailing blanks in the
 SELECT command. PostgreSQL has a trim(trailing ' ' from column)', but
 MS-SQL upchucks on that syntax.

 --
 There is nothing more pleasant than traveling and meeting new people!
 Genghis Khan

 Maranatha! 
 John McKown

 __
 R-help@r-project.org mailing list
 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.

 __
 R-help@r-project.org mailing list
 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.



-- 
http://had.co.nz/

__
R-help@r-project.org mailing list
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.


[R] String comparison, trailing blanks make a difference.

2014-07-18 Thread John McKown
Well, this was a shock to me. And I don't really see any documentation
about it, but perhaps I just can't see it.

abc == abc 
[1] FALSE

I guess that I thought of strings in R like I do is some other
languages where the shorter value is padded with blanks to the length
of the longer value, then compared. I.e. that trailing blanks didn't
matter.

The best solution that I have found is to use the str_trim() function
from the stringr to remove all the trailing blanks after I get the
data from the SQL data base. I cannot change the SQL schema to make
the column a varchar instead of a char column. It is a vendor DB. And
I don't know an ANSI SQL standard way to remove trailing blanks in the
SELECT command. PostgreSQL has a trim(trailing ' ' from column)', but
MS-SQL upchucks on that syntax.

-- 
There is nothing more pleasant than traveling and meeting new people!
Genghis Khan

Maranatha! 
John McKown

__
R-help@r-project.org mailing list
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.


Re: [R] String comparison, trailing blanks make a difference.

2014-07-18 Thread William Dunlap
abc == abc 
 [1] FALSE

R does no interpretation of strings when doing comparisons so you do
have do your own canonicalization.  That may involve removing
trailing, leading, or all white space or punctuation, converting to
lower or upper case, mapping nicknames to official names, trimming to
a fixed number of characters, etc.

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Fri, Jul 18, 2014 at 9:17 AM, John McKown
john.archie.mck...@gmail.com wrote:
 Well, this was a shock to me. And I don't really see any documentation
 about it, but perhaps I just can't see it.

abc == abc 
 [1] FALSE

 I guess that I thought of strings in R like I do is some other
 languages where the shorter value is padded with blanks to the length
 of the longer value, then compared. I.e. that trailing blanks didn't
 matter.

 The best solution that I have found is to use the str_trim() function
 from the stringr to remove all the trailing blanks after I get the
 data from the SQL data base. I cannot change the SQL schema to make
 the column a varchar instead of a char column. It is a vendor DB. And
 I don't know an ANSI SQL standard way to remove trailing blanks in the
 SELECT command. PostgreSQL has a trim(trailing ' ' from column)', but
 MS-SQL upchucks on that syntax.

 --
 There is nothing more pleasant than traveling and meeting new people!
 Genghis Khan

 Maranatha! 
 John McKown

 __
 R-help@r-project.org mailing list
 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.

__
R-help@r-project.org mailing list
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.


Re: [R] String comparison, trailing blanks make a difference.

2014-07-18 Thread Hervé Pagès

Hi John,

On 07/18/2014 09:17 AM, John McKown wrote:

Well, this was a shock to me. And I don't really see any documentation
about it, but perhaps I just can't see it.


abc == abc

[1] FALSE

I guess that I thought of strings in R like I do is some other
languages where the shorter value is padded with blanks to the length
of the longer value, then compared. I.e. that trailing blanks didn't
matter.


The shock to me is to learn that some programming languages consider
strings abc and abc  to be the same. Please name them so I can stay
away from them ;-)

Thanks,
H.



The best solution that I have found is to use the str_trim() function
from the stringr to remove all the trailing blanks after I get the
data from the SQL data base. I cannot change the SQL schema to make
the column a varchar instead of a char column. It is a vendor DB. And
I don't know an ANSI SQL standard way to remove trailing blanks in the
SELECT command. PostgreSQL has a trim(trailing ' ' from column)', but
MS-SQL upchucks on that syntax.



--
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpa...@fhcrc.org
Phone:  (206) 667-5791
Fax:(206) 667-1319

__
R-help@r-project.org mailing list
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.


Re: [R] String substitution

2013-10-04 Thread irene
Wonderful!

Thank you Arun!

Irene
 
Irene Ruberto





 Da: arun kirshna [via R] ml-node+s789695n4677558...@n4.nabble.com

Inviato: Giovedì 3 Ottobre 2013 22:51
Oggetto: Re: String substitution



Hi, 
Try: 
dat$y- as.character(dat$y) 
dat1- dat 
dat2- dat 
library(stringr) 
 dat$y[NET]- substr(word(dat$y[NET],2),1,1) 
 dat$y 
#[1] n     n     house n     tree 

#or 
for(i in 1:length(NET)){dat1$y[NET[i]]- n} 
 dat1$y 
#[1] n     n     house n     tree 
#or 
dat2$y[NET]- gsub(.*(n).*,\\1,dat2$y[NET]) 
 dat2$y 
#[1] n     n     house n     tree 


A.K. 


Hello, 

I am trying to replace strings containing a certain word, I 
first identified the word (in this example net) with grep, and then I 
need to replace those string with n. It should be very simple but I 
don't seem to find the solution. 
  
Example: 

x-c(5:9) 
y- c(with net, with nets, house, no nets, tree) 
dat-as.data.frame(cbind(x, y) ) 
NET-grep(net, dat$y) 

# I want y to become (n, n, house, n, tree) # 

# I have tried several ways including the following but without success # 

for (i in 1: length(NET)) { 
dat$y[NET[i]]- n } 


Thank you for your help! 

__ 
[hidden email] mailing list 
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. 




If you reply to this email, your message will be added to the discussion 
below:http://r.789695.n4.nabble.com/String-substitution-tp4677541p4677558.html 
To unsubscribe from String substitution, click here.
NAML



--
View this message in context: 
http://r.789695.n4.nabble.com/String-substitution-tp4677541p4677612.html
Sent from the R help mailing list archive at Nabble.com.
[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.


Re: [R] String substitution

2013-10-03 Thread arun
Hi,
Try:
dat$y- as.character(dat$y)
dat1- dat
dat2- dat
library(stringr)
 dat$y[NET]- substr(word(dat$y[NET],2),1,1)
 dat$y
#[1] n n house n tree 

#or
for(i in 1:length(NET)){dat1$y[NET[i]]- n}
 dat1$y
#[1] n n house n tree 
#or
dat2$y[NET]- gsub(.*(n).*,\\1,dat2$y[NET])
 dat2$y
#[1] n n house n tree 


A.K.


Hello, 

I am trying to replace strings containing a certain word, I 
first identified the word (in this example net) with grep, and then I 
need to replace those string with n. It should be very simple but I 
don't seem to find the solution. 
  
Example: 

x-c(5:9) 
y- c(with net, with nets, house, no nets, tree) 
dat-as.data.frame(cbind(x, y) ) 
NET-grep(net, dat$y) 

# I want y to become (n, n, house, n, tree) # 

# I have tried several ways including the following but without success # 

for (i in 1: length(NET)) { 
dat$y[NET[i]]- n } 


Thank you for your help!

__
R-help@r-project.org mailing list
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.


[R] string processing(regular expressions)

2013-09-01 Thread Robert Lynch
I have a variable that is course #
nCourse -
as.factor(c(002A,002B,002C,007A,007B,007C,101,118A,118B,118C))

And I would like to get rid of the leading zeros, and have the following
set
(2A,2B,2C,7A,7B,7C,101,118A,118B,118C) to paste()
together with the department, B,P,C (bio, phys,  chem etc)

I am stuck trying to figure out regular expressions, they are new to me.

Thank You very much

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.


Re: [R] string processing(regular expressions)

2013-09-01 Thread Rui Barradas

Hello,

Try the following.

gsub(^0+, , as.character(nCourse))


Hope this helps,

Rui Barradas

Em 01-09-2013 21:41, Robert Lynch escreveu:

I have a variable that is course #
nCourse -
as.factor(c(002A,002B,002C,007A,007B,007C,101,118A,118B,118C))

And I would like to get rid of the leading zeros, and have the following
set
(2A,2B,2C,7A,7B,7C,101,118A,118B,118C) to paste()
together with the department, B,P,C (bio, phys,  chem etc)

I am stuck trying to figure out regular expressions, they are new to me.

Thank You very much

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.



__
R-help@r-project.org mailing list
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.


Re: [R] string processing(regular expressions)

2013-09-01 Thread arun
Hi,
 levels(nCourse)-gsub(^0+,,levels(nCourse))
 nCourse
# [1] 2A   2B   2C   7A   7B   7C   101  118A 118B 118C
#Levels: 2A 2B 2C 7A 7B 7C 101 118A 118B 118C

#The second part is not very clear.
res-setNames(data.frame(lapply(c(B,P,C),function(x) 
paste0(levels(nCourse),x)),stringsAsFactors=FALSE),c(B,P,C))
head(res)
#    B   P   C
#1 2AB 2AP 2AC
#2 2BB 2BP 2BC
#3 2CB 2CP 2CC
#4 7AB 7AP 7AC
#5 7BB 7BP 7BC
#6 7CB 7CP 7CC
A.K.




- Original Message -
From: Robert Lynch robert.b.ly...@gmail.com
To: R help r-help@r-project.org
Cc: 
Sent: Sunday, September 1, 2013 4:41 PM
Subject: [R] string processing(regular expressions)

I have a variable that is course #
nCourse -
as.factor(c(002A,002B,002C,007A,007B,007C,101,118A,118B,118C))

And I would like to get rid of the leading zeros, and have the following
set
(2A,2B,2C,7A,7B,7C,101,118A,118B,118C) to paste()
together with the department, B,P,C (bio, phys,  chem etc)

I am stuck trying to figure out regular expressions, they are new to me.

Thank You very much

    [[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.


__
R-help@r-project.org mailing list
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.


[R] String based chemical name identification

2013-07-03 Thread Zsurzsa Laszlo
The problem is the following:

I have two big databases one look like this:

  2-Methyl-4-trimethylsilyloxyoct-5-yne   Benzoic acid, methyl ester   Benzoic
acid, 2-methyl-, methyl ester   Acetic acid, phenylmethyl ester
 2,7-Dimethyl-4-trimethylsilyloxyoct-7-en-5-yne   etc.

The second one looks like this:

 Name: D-Tagatose 1,6-bisphosphate  Name: 1-Phosphatidyl-D-myo-inositol;:
1-Phosphatidyl-1D-myo-inositol;: 1-Phosphatidyl-myo-inositol;:
Phosphatidyl-1D-myo-inositol;: (3-Phosphatidyl)-1-D-inositol;:
1,2-Diacyl-sn-glycero-3-phosphoinositol;: Phosphatidylinositol  Name:
Androstenedione;: Androst-4-ene-3,17-dione;: 4-Androstene-3,17-dione  Name:
Spermine;: N,N'-Bis(3-aminopropyl)-1,4-butanediamine  Name: H+;: Hydron  Name:
3-Iodo-L-tyrosine  etc.

Both of them have more then 3000 lines. Matching their name by hand is not
an option because I don't know chemistry.

*Possible solution I came up with*:

Go through all the names of the first database and then try to match with
the other one. I'm using *regexec *and *strsplit *functions for the
matching. Basically I split the name into small chunks and try to get some
hit in the other database.

I can supply code If needed but I did not want to spam in the first mail.


Any solution is welcome! It can be in pseudo-cod also or in any type of
logical arguing. It does not matter.


Laszlo-Andras Zsurzsa

Msc. Informatics, Technical University Munchen

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.


Re: [R] String based chemical name identification

2013-07-03 Thread Law, Jason
Might be better off using a web service like ChemSpider to do the matching for 
you http://www.chemspider.com/AboutServices.aspx?.  The idea that you can 
identify the synonyms by name is probably optimistic unless they are exact 
matches.

Here's some python code that seems to make it pretty easy: 
https://github.com/mcs07/ChemSpiPy.  Search the names, extract the InChI for 
the best match and then you can match them in R via the InChI.  Might require 
some fixing by hand afterwards.

HTH,

Jason Law

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Zsurzsa Laszlo
Sent: Wednesday, July 03, 2013 7:28 AM
To: r-help@r-project.org
Subject: [R] String based chemical name identification

The problem is the following:

I have two big databases one look like this:

  2-Methyl-4-trimethylsilyloxyoct-5-yne   Benzoic acid, methyl ester   Benzoic
acid, 2-methyl-, methyl ester   Acetic acid, phenylmethyl ester
 2,7-Dimethyl-4-trimethylsilyloxyoct-7-en-5-yne   etc.

The second one looks like this:

 Name: D-Tagatose 1,6-bisphosphate  Name: 1-Phosphatidyl-D-myo-inositol;:
1-Phosphatidyl-1D-myo-inositol;: 1-Phosphatidyl-myo-inositol;:
Phosphatidyl-1D-myo-inositol;: (3-Phosphatidyl)-1-D-inositol;:
1,2-Diacyl-sn-glycero-3-phosphoinositol;: Phosphatidylinositol  Name:
Androstenedione;: Androst-4-ene-3,17-dione;: 4-Androstene-3,17-dione  Name:
Spermine;: N,N'-Bis(3-aminopropyl)-1,4-butanediamine  Name: H+;: Hydron  Name:
3-Iodo-L-tyrosine  etc.

Both of them have more then 3000 lines. Matching their name by hand is not an 
option because I don't know chemistry.

*Possible solution I came up with*:

Go through all the names of the first database and then try to match with the 
other one. I'm using *regexec *and *strsplit *functions for the matching. 
Basically I split the name into small chunks and try to get some hit in the 
other database.

I can supply code If needed but I did not want to spam in the first mail.


Any solution is welcome! It can be in pseudo-cod also or in any type of logical 
arguing. It does not matter.


Laszlo-Andras Zsurzsa

Msc. Informatics, Technical University Munchen

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.

__
R-help@r-project.org mailing list
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.


[R] string size limits in RCurl

2013-04-24 Thread Elmore, Ryan
Hi All,

I am running into what appears to be character size limit in a JSON string when 
trying retrieve data from either `curlPerform()` or `getURL()`.  Here is 
non-reproducible code [1], but it should shed some light on the problem.

# Note that .base.url is the basic url for the API, q is a query, user
#  is specified, etc.
session = getCurlHandle()
curl.opts - list(userpwd = paste(user, :, key, sep = ),
  httpheader = Content-Type: application/json)
request - paste(.base.url, q, sep = )
txt - getURL(url = request, curl = session, .opts = curl.opts,
  write = basicTextGatherer())

or

r = dynCurlReader()
curlPerform(url = request, writefunction = r$update, curl = session,
.opts = curl.opts)

My guess is that the `update` or `value` functions in the `basicTextGather` or 
`dynCurlReader` text handler objects are having trouble with the large strings. 
 In this example, `r$value()` will return a truncated string that is 
approximately 2 MB.  The code given above will work fine for queries  2 MB.

Note that I can easily do the following from the command line (or using 
`system()` in R), but writing to disc seems like a waste if I am doing the 
subsequent analysis in R.

curl -v --header Content-Type: application/json --user 
username:register:passwd 
https://base.url.for.api/getdata/select+*+from+sometable  stream.json

where `file.json` is a roughly 14MB json string. I can read the string into R 
using either

con - file(paste(.project.path, data/stream.json, sep = ), r)
string - readLines(con)

or directly to list as

tmp - fromJSON(file = paste(.project.path, data/stream.json, sep = ))

Any thoughts are very much appreciated.  Note that I posted this same 
question/comment to StackOverflow and will happily provide any helpful 
suggestions to that list as well.

Ryan

[1] - Sorry for not providing reproducible code, but I'm dealing with a govt 
firewall.

__
R-help@r-project.org mailing list
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.


[R] string split at xth position

2013-03-13 Thread Johannes Radinger
Hi,

I have a vector of strings like:
c(a1b1,a2b2,a1b2) which I want to spilt into two parts like:
c(a1,a2,a2) and c(b1,b2,b2). So there is
always a first part with a+number and a second part with b+number.
Unfortunately there is no separator I could use to directly split
the vectors.. Any idea how to handle such cases?

/Johannes

__
R-help@r-project.org mailing list
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.


Re: [R] string split at xth position

2013-03-13 Thread Jorge I Velez
Dear Johannes,

May not be the best way, but this looks like what you described:

 x - c(a1b1,a2b2,a1b2)
 x
[1] a1b1 a2b2 a1b2
 substr(x, 1, 2)
[1] a1 a2 a1
 substr(x, 3, 4)
[1] b1 b2 b2

HTH,
Jorge.-


On Wed, Mar 13, 2013 at 7:37 PM, Johannes Radinger  wrote:

 Hi,

 I have a vector of strings like:
 c(a1b1,a2b2,a1b2) which I want to spilt into two parts like:
 c(a1,a2,a2) and c(b1,b2,b2). So there is
 always a first part with a+number and a second part with b+number.
 Unfortunately there is no separator I could use to directly split
 the vectors.. Any idea how to handle such cases?

 /Johannes

 __
 R-help@r-project.org mailing list
 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.


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.


Re: [R] string split at xth position

2013-03-13 Thread Johannes Radinger
Thank you Jorge!

thats working perfectly...

/johannes



On Wed, Mar 13, 2013 at 9:45 AM, Jorge I Velez jorgeivanve...@gmail.com wrote:
 Dear Johannes,

 May not be the best way, but this looks like what you described:

 x - c(a1b1,a2b2,a1b2)
 x
 [1] a1b1 a2b2 a1b2
 substr(x, 1, 2)
 [1] a1 a2 a1
 substr(x, 3, 4)
 [1] b1 b2 b2

 HTH,
 Jorge.-


 On Wed, Mar 13, 2013 at 7:37 PM, Johannes Radinger  wrote:

 Hi,

 I have a vector of strings like:
 c(a1b1,a2b2,a1b2) which I want to spilt into two parts like:
 c(a1,a2,a2) and c(b1,b2,b2). So there is
 always a first part with a+number and a second part with b+number.
 Unfortunately there is no separator I could use to directly split
 the vectors.. Any idea how to handle such cases?

 /Johannes

 __
 R-help@r-project.org mailing list
 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.



__
R-help@r-project.org mailing list
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.


Re: [R] string split at xth position

2013-03-13 Thread arun


Hi,

You could use:
library(stringr)
?str_sub()
lapply(2:3,function(i) if(i==2) str_sub(x,end=i) else str_sub(x,i))
#[[1]]
#[1] a1 a2 a1

#[[2]]
#[1] b1 b2 b2
A.K.




- Original Message -
From: Johannes Radinger johannesradin...@gmail.com
To: r-help@r-project.org
Cc: 
Sent: Wednesday, March 13, 2013 4:37 AM
Subject: [R] string split at xth position

Hi,

I have a vector of strings like:
c(a1b1,a2b2,a1b2) which I want to spilt into two parts like:
c(a1,a2,a2) and c(b1,b2,b2). So there is
always a first part with a+number and a second part with b+number.
Unfortunately there is no separator I could use to directly split
the vectors.. Any idea how to handle such cases?

/Johannes

__
R-help@r-project.org mailing list
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.

__
R-help@r-project.org mailing list
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.


[R] string split at xth position

2013-03-13 Thread Keith Weintraub

Here is another way

require(stringr)

aaa-paste0(a, 1:20)
bbb-paste0(b, 101:120)
ab-paste0(aaa,bbb)
ab


ptrn-([ab][[:digit:]]*)
unlist(str_extract_all(ab, ptrn))



 Hi,
 
 I have a vector of strings like:
 c(a1b1,a2b2,a1b2) which I want to spilt into two parts like:
 c(a1,a2,a2) and c(b1,b2,b2). So there is
 always a first part with a+number and a second part with b+number.
 Unfortunately there is no separator I could use to directly split
 the vectors.. Any idea how to handle such cases?
 
 /Johannes


--

__
R-help@r-project.org mailing list
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.


Re: [R] string split at xth position

2013-03-13 Thread arun
Hi,

If you have cases like these:
 x1- c(a1b11,a10b2,a2b2,a140b31)
 lapply(list(c(1,2),c(3,4)),function(i) substr(x1,i[1],i[2])) #it will not work
#[[1]]
#[1] a1 a1 a2 a1

#[[2]]
#[1] b1 0b b2 40


let1-unique(unlist(strsplit(gsub(\\d+,,x1),)))
 split(unlist(strsplit(gsub((\\w\\d+)(\\w\\d+),\\1 \\2,x1), )),let1)
#$a
#[1] a1   a10  a2   a140

#$b
#[1] b11 b2  b2  b31
A.K.




- Original Message -
From: arun smartpink...@yahoo.com
To: Johannes Radinger johannesradin...@gmail.com
Cc: 
Sent: Wednesday, March 13, 2013 8:32 AM
Subject: Re: [R] string split at xth position

Also,
You could try:
 lapply(list(c(1,2),c(3,4)),function(i) substr(x,i[1],i[2]))
#[[1]]
#[1] a1 a2 a1

#[[2]]
#[1] b1 b2 b2
A.K.



- Original Message -
From: Johannes Radinger johannesradin...@gmail.com
To: r-help@r-project.org
Cc: 
Sent: Wednesday, March 13, 2013 4:37 AM
Subject: [R] string split at xth position

Hi,

I have a vector of strings like:
c(a1b1,a2b2,a1b2) which I want to spilt into two parts like:
c(a1,a2,a2) and c(b1,b2,b2). So there is
always a first part with a+number and a second part with b+number.
Unfortunately there is no separator I could use to directly split
the vectors.. Any idea how to handle such cases?

/Johannes

__
R-help@r-project.org mailing list
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.

__
R-help@r-project.org mailing list
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.


Re: [R] String Handling() for Split a word by a letter

2012-08-28 Thread Jim Lemon

On 08/27/2012 07:29 PM, Rantony wrote:

Hi,

here im unable to run a string handle function called unpaste().

for eg:- a- 12345_mydata
Actually my requirement what is i need to get , only 12345.  Means that , i
need the all letter as a word which is before of  first   _  - symbol of
a. i tried to do with unpaste, it says function not found.  After that i
tried with strsplit() . it ran, but again i need to write another method
to get again after spliting.

Any other good method is there for this requirement ?


Hi Antony,
You probably want:

strsplit(a,_)[[1]][1]

which can be used for more than one input string

a-paste(sample(12345:13456,10),mydata,sep=_)
sapply(strsplit(a,_),[,1)

Jim

__
R-help@r-project.org mailing list
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.


Re: [R] String Handling() for Split a word by a letter

2012-08-28 Thread Rantony
Here I have a variable called Variable_1.

 

Variable_1 - MyDataFrame

 

Here I want to create another variable, by assigning the value of
Variable_1 .

So, it will come like,

 

Assign(Variable_1,data.frame(read.csv(c:\\Mydata.csv)))   ---[this
was the 1st requirement, now I got the solution]

 

Now,  MyDataFrame  is a variable and containing some values in that.

And Now, the problem what is I need to do rbind into the variable 
MyDataFrame .

 

I tried to do,

rbind(as.character(Variable_1),
data.frame(read.csv(c:\\My2ndData.csv)))

 

and I tried this too

 

rbind(as.name(Variable_1), data.frame(read.csv(c:\\My2ndData.csv)))

 

and I getting the error like

 

Error:  object of type 'symbol' is not subsettable 

 

 

Here I am getting stuck !

 

 

- Thanks

Antony

 

 

From: jholtman [via R] [mailto:ml-node+s789695n4641417...@n4.nabble.com]

Sent: Monday, August 27, 2012 7:56 PM
To: Akkara, Antony (GE Energy, Non-GE)
Subject: Re: String Handling() for Split a word by a letter

 

Is this what you want: 


 a- 12345_mydata 
 sub(_.*, , a) 
[1] 12345 


On Mon, Aug 27, 2012 at 5:29 AM, Rantony [hidden email] wrote: 


 Hi, 
 
 here im unable to run a string handle function called unpaste(). 
 
 for eg:- a- 12345_mydata 
 Actually my requirement what is i need to get , only 12345.  Means
that , i 
 need the all letter as a word which is before of  first   _  -
symbol of 
 a. i tried to do with unpaste, it says function not found.  After
that i 
 tried with strsplit() . it ran, but again i need to write another
method 
 to get again after spliting. 
 
 Any other good method is there for this requirement ? 
 
 - Thanks 
 Antony. 
 
 
 
 -- 
 View this message in context: 

http://r.789695.n4.nabble.com/String-Handling-for-Split-a-word-by-a-lett
er-tp4641397.html
 Sent from the R help mailing list archive at Nabble.com. 
 
 __ 
 [hidden email] mailing list 
 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. 
 




-- 
Jim Holtman 
Data Munger Guru 

What is the problem that you are trying to solve? 
Tell me what you want to do, not how you want to do it. 

[[alternative HTML version deleted]] 

__ 
[hidden email] mailing list 
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. 





If you reply to this email, your message will be added to the discussion
below:

http://r.789695.n4.nabble.com/String-Handling-for-Split-a-word-by-a-lett
er-tp4641397p4641417.html 

To unsubscribe from String Handling() for Split a word by a letter,
click here
http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscrib
e_by_codenode=4641397code=YW50b255LmFra2FyYUBnZS5jb218NDY0MTM5N3wxNTUx
OTQzMDI5 .
NAML
http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_view
erid=instant_html%21nabble%3Aemail.namlbase=nabble.naml.namespaces.Bas
icNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.tem
plate.NodeNamespacebreadcrumbs=notify_subscribers%21nabble%3Aemail.naml
-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemai
l.naml  





--
View this message in context: 
http://r.789695.n4.nabble.com/String-Handling-for-Split-a-word-by-a-letter-tp4641397p4641534.html
Sent from the R help mailing list archive at Nabble.com.
[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.


[R] String Handling() for Split a word by a letter

2012-08-27 Thread Rantony
Hi,

here im unable to run a string handle function called unpaste().

for eg:- a- 12345_mydata
Actually my requirement what is i need to get , only 12345.  Means that , i
need the all letter as a word which is before of  first   _  - symbol of 
a. i tried to do with unpaste, it says function not found.  After that i
tried with strsplit() . it ran, but again i need to write another method
to get again after spliting. 

Any other good method is there for this requirement ?

- Thanks
Antony.



--
View this message in context: 
http://r.789695.n4.nabble.com/String-Handling-for-Split-a-word-by-a-letter-tp4641397.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
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.


Re: [R] String Handling() for Split a word by a letter

2012-08-27 Thread jim holtman
Is this what you want:


 a- 12345_mydata
 sub(_.*, , a)
[1] 12345


On Mon, Aug 27, 2012 at 5:29 AM, Rantony antony.akk...@ge.com wrote:

 Hi,

 here im unable to run a string handle function called unpaste().

 for eg:- a- 12345_mydata
 Actually my requirement what is i need to get , only 12345.  Means that , i
 need the all letter as a word which is before of  first   _  - symbol of
 a. i tried to do with unpaste, it says function not found.  After that i
 tried with strsplit() . it ran, but again i need to write another method
 to get again after spliting.

 Any other good method is there for this requirement ?

 - Thanks
 Antony.



 --
 View this message in context:
 http://r.789695.n4.nabble.com/String-Handling-for-Split-a-word-by-a-letter-tp4641397.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@r-project.org mailing list
 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.




-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.


Re: [R] String Handling() for Split a word by a letter

2012-08-27 Thread Rui Barradas

Hello,

If the op says he has tried strsplit, maybe a simple subsetting 
afterwards would solve it.


a - 12345_mydata
strsplit(a, _)[[1]][1]

Hope this helps,

Rui Barradas

Em 27-08-2012 15:22, jim holtman escreveu:

Is this what you want:



a- 12345_mydata
sub(_.*, , a)

[1] 12345


On Mon, Aug 27, 2012 at 5:29 AM, Rantony antony.akk...@ge.com wrote:


Hi,

here im unable to run a string handle function called unpaste().

for eg:- a- 12345_mydata
Actually my requirement what is i need to get , only 12345.  Means that , i
need the all letter as a word which is before of  first   _  - symbol of
a. i tried to do with unpaste, it says function not found.  After that i
tried with strsplit() . it ran, but again i need to write another method
to get again after spliting.

Any other good method is there for this requirement ?

- Thanks
Antony.



--
View this message in context:
http://r.789695.n4.nabble.com/String-Handling-for-Split-a-word-by-a-letter-tp4641397.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
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.






__
R-help@r-project.org mailing list
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.


[R] String Manipulation in R

2012-06-12 Thread anjali
Hi ,
Is there any inbuilt functions  to check whether a substring is present in a
string and give the result as boolean 
Thanks 


--
View this message in context: 
http://r.789695.n4.nabble.com/String-Manipulation-in-R-tp4633104.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
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.


Re: [R] String Manipulation in R

2012-06-12 Thread R. Michael Weylandt
grepl

Michael

On Tue, Jun 12, 2012 at 8:51 AM, anjali jeevi...@gmail.com wrote:
 Hi ,
 Is there any inbuilt functions  to check whether a substring is present in a
 string and give the result as boolean
 Thanks


 --
 View this message in context: 
 http://r.789695.n4.nabble.com/String-Manipulation-in-R-tp4633104.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@r-project.org mailing list
 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.

__
R-help@r-project.org mailing list
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.


Re: [R] String Manipulation in R

2012-06-12 Thread Jeff Newmiller
?grepl

Note that this function uses regular expressions, in which certain characters 
have special meanings, so depending on what string you are looking for you may 
have to know something about regex patterns to get it to work.
---
Jeff NewmillerThe .   .  Go Live...
DCN:jdnew...@dcn.davis.ca.usBasics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

anjali jeevi...@gmail.com wrote:

Hi ,
Is there any inbuilt functions  to check whether a substring is present
in a
string and give the result as boolean 
Thanks 


--
View this message in context:
http://r.789695.n4.nabble.com/String-Manipulation-in-R-tp4633104.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
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.

__
R-help@r-project.org mailing list
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.


Re: [R] String Manipulation in R

2012-06-12 Thread Rui Barradas

Hello,

Yes, there is. See ?grepl or help('grepl').

Hope this helps,

Rui Barradas

Em 12-06-2012 14:51, anjali escreveu:

Hi ,
Is there any inbuilt functions  to check whether a substring is present in a
string and give the result as boolean
Thanks


--
View this message in context: 
http://r.789695.n4.nabble.com/String-Manipulation-in-R-tp4633104.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
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.



__
R-help@r-project.org mailing list
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.


Re: [R] String Manipulation in R

2012-06-12 Thread Greg Snow
Or use 'fixed=TRUE' as an argument to grepl to avoid the regular
expression matching (but learning regular expressions will be a useful
tool in the long run).

On Tue, Jun 12, 2012 at 9:15 AM, Jeff Newmiller
jdnew...@dcn.davis.ca.us wrote:
 ?grepl

 Note that this function uses regular expressions, in which certain characters 
 have special meanings, so depending on what string you are looking for you 
 may have to know something about regex patterns to get it to work.
 ---
 Jeff Newmiller                        The     .       .  Go Live...
 DCN:jdnew...@dcn.davis.ca.us        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
 Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
 /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
 ---
 Sent from my phone. Please excuse my brevity.

 anjali jeevi...@gmail.com wrote:

Hi ,
Is there any inbuilt functions  to check whether a substring is present
in a
string and give the result as boolean
Thanks


--
View this message in context:
http://r.789695.n4.nabble.com/String-Manipulation-in-R-tp4633104.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
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.

 __
 R-help@r-project.org mailing list
 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.



-- 
Gregory (Greg) L. Snow Ph.D.
538...@gmail.com

__
R-help@r-project.org mailing list
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.


Re: [R] string substitution for argument in function

2012-03-26 Thread Prof. Dr. Pedro Martinez Arbizu

Me again,
what I really dont understand is the behaivour of R here.
notice, elem is not written in brackets, why it is interpreted as 
string and not as a variable?
I would expect an error elem not found because it is not defined as 
variable, but in contrary it is accepted as column name, see below:


 a = c('one','two','three')
 data =c()
 data=cbind(elem = 2,data)
 data
 elem
[1,]2

 data=cbind('elem' = 2,data)
 data
 elem elem
[1,]22

 elem
Fehler: Objekt 'elem' nicht gefunden

pedro








On 26.03.2012 17:22, Prof. Dr. Pedro Martinez Arbizu wrote:

HI, thanks Weidong, Henrique,
this works for the example (may be a bad example, but it should be as 
simple as possible),


This is however only a workaround to name the columns of a dataframe, 
but is not addressing the problem itself, that is, how can I 
substitute a string (used as argument in a function) within a function 
call in a for-loop??


I dont know if I have explain myself,

something like this works well:

 a = c('one','two','three')


 data= c()
 for(elem in a){data=cbind('title' = elem,data)}
 data

 title   title title
[1,] three two one


So I can use 'title' = elem within the function and it will substitute 
elem by a name, every loop by a different name of a list.


...but I cannot use the contrary:   elem = 'something' within the 
function call.


My real life problem is using randomForest() I want to balance the 
class frequency with the argument sampsize = c( 'absent' = 23, elem = 23)

where elem is everytime a different name taken from a list in a for-loop.

Regards Pedro



On 26.03.2012 02:17, Weidong Gu wrote:

Hi,

This may help

  a = c('one','two','three')
data.frame(eval(substitute(rbind(var,2),list(var=a

?substitute
?eval

Weidong Gu


On Sun, Mar 25, 2012 at 5:22 PM, Pedro Martinez
pedro.marti...@senckenberg.de  wrote:

hello,
I want to iterate through a list of names and use each element as an
argument in a function. For instance:


a = c('one','two','three')
data= c()
for(elem in a){data=cbind(elem = 2,data)}
data

 elem elem elem
[1,]222

instead I want 'elem' to be substituted by the string in the list. 
Doing

it by hand would be:

data = cbind('one'=2,data)
data = cbind('two'=2,data)
data = cbind('three'=2,data)
data

 'one' 'two' 'three'
[1,]222

I guess that the clue would be in sub(),gsub(), paste() or similar 
but I

didnt get it to work.

I am comming from python were we woudl do something like:

a = ['one','two','three']
data = {}
for elem in a:
   data[elem] = 2
data

{'three': 2, 'two': 2, 'one': 2}

Thanks, Pedro


-
Prof. Dr. P. Martinez Arbizu
DZMB-Forschungsinstitut Senckenberg

Suedstrand 44
D-26382 Wilhelmshaven
Germany

Tel: +49 (0)4421 9475-100
Fax: +49 (0)4421 9475-111

Email: pmarti...@senckenberg.de

Senckenberg Gesellschaft für Naturforschung
Rechtsfähiger Verein gemäß § 22 BGB
Senckenberganlage 25
60325 Frankfurt
Direktorium: Prof. Dr. Dr. h.c. Volker Mosbrugger, Prof. Dr. Michael
Türkay, Dr. Johannes Heilmann, Prof. Dr. Pedro Martinez Arbizu, Prof.
Dr. Georg Zizka, Prof. Dr. Uwe Fritz
Vorsitzender des Präsidiums: Dietmar Schmid
Aufsichtsbehörde: Magistrat der Stadt Frankfurt am Main (Ordnungsamt)

__
R-help@r-project.org mailing list
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.






--
-
Prof. Dr. Pedro Martinez Arbizu

Deutsches Zentrum für Marine Biodiversitätsforschung
Senckenberg am Meer
Südstrand 44
26382 Wilhelmshaven
Tel +49 (0)4421 9475 100
Fax +49 (0)4421 9475 111
Email: pmarti...@senckenberg.de
http://www.senckenberg.de/root/index.php?page_id=2141

und

C.v.O. Universität Oldenburg
FK V, Institut für Biologie u. Umweltwissenschaften
Ag Marine Biodiversität

**
Senckenberg Gesellschaft für Naturforschung
Rechtsfähiger Verein gemäß § 22 BGB
Senckenberganlage 25
60325 Frankfurt
Direktorium: Prof. Dr. Dr. h.c. Volker Mosbrugger, Prof. Dr. Michael Türkay, 
Dr. Johannes Heilmann, Prof. Dr. Pedro Martinez Arbizu, Prof. Dr. Georg Zizka, 
Prof. Dr. Uwe Fritz
Vorsitzender des Präsidiums: Dietmar Schmid
Aufsichtsbehörde: Magistrat der Stadt Frankfurt am Main (Ordnungsamt)

__
R-help@r-project.org mailing list
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.


Re: [R] string substitution for argument in function

2012-03-26 Thread Prof. Dr. Pedro Martinez Arbizu

HI, thanks Weidong, Henrique,
this works for the example (may be a bad example, but it should be as 
simple as possible),


This is however only a workaround to name the columns of a dataframe, 
but is not addressing the problem itself, that is, how can I substitute 
a string (used as argument in a function) within a function call in a 
for-loop??


I dont know if I have explain myself,

something like this works well:

 a = c('one','two','three')


 data= c()
 for(elem in a){data=cbind('title' = elem,data)}
 data

 title   title title
[1,] three two one


So I can use 'title' = elem within the function and it will substitute 
elem by a name, every loop by a different name of a list.


...but I cannot use the contrary:   elem = 'something' within the 
function call.


My real life problem is using randomForest() I want to balance the class 
frequency with the argument sampsize = c( 'absent' = 23, elem = 23)

where elem is everytime a different name taken from a list in a for-loop.

Regards Pedro



On 26.03.2012 02:17, Weidong Gu wrote:

Hi,

This may help

  a = c('one','two','three')
data.frame(eval(substitute(rbind(var,2),list(var=a

?substitute
?eval

Weidong Gu


On Sun, Mar 25, 2012 at 5:22 PM, Pedro Martinez
pedro.marti...@senckenberg.de  wrote:

hello,
I want to iterate through a list of names and use each element as an
argument in a function. For instance:


a = c('one','two','three')
data= c()
for(elem in a){data=cbind(elem = 2,data)}
data

 elem elem elem
[1,]222

instead I want 'elem' to be substituted by the string in the list. Doing
it by hand would be:

data = cbind('one'=2,data)
data = cbind('two'=2,data)
data = cbind('three'=2,data)
data

 'one' 'two' 'three'
[1,]222

I guess that the clue would be in sub(),gsub(), paste() or similar but I
didnt get it to work.

I am comming from python were we woudl do something like:

a = ['one','two','three']
data = {}
for elem in a:
   data[elem] = 2
data

{'three': 2, 'two': 2, 'one': 2}

Thanks, Pedro


-
Prof. Dr. P. Martinez Arbizu
DZMB-Forschungsinstitut Senckenberg

Suedstrand 44
D-26382 Wilhelmshaven
Germany

Tel: +49 (0)4421 9475-100
Fax: +49 (0)4421 9475-111

Email: pmarti...@senckenberg.de

Senckenberg Gesellschaft für Naturforschung
Rechtsfähiger Verein gemäß § 22 BGB
Senckenberganlage 25
60325 Frankfurt
Direktorium: Prof. Dr. Dr. h.c. Volker Mosbrugger, Prof. Dr. Michael
Türkay, Dr. Johannes Heilmann, Prof. Dr. Pedro Martinez Arbizu, Prof.
Dr. Georg Zizka, Prof. Dr. Uwe Fritz
Vorsitzender des Präsidiums: Dietmar Schmid
Aufsichtsbehörde: Magistrat der Stadt Frankfurt am Main (Ordnungsamt)

__
R-help@r-project.org mailing list
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.



--
-
Prof. Dr. Pedro Martinez Arbizu

Deutsches Zentrum für Marine Biodiversitätsforschung
Senckenberg am Meer
Südstrand 44
26382 Wilhelmshaven
Tel +49 (0)4421 9475 100
Fax +49 (0)4421 9475 111
Email: pmarti...@senckenberg.de
http://www.senckenberg.de/root/index.php?page_id=2141

und

C.v.O. Universität Oldenburg
FK V, Institut für Biologie u. Umweltwissenschaften
Ag Marine Biodiversität

**
Senckenberg Gesellschaft für Naturforschung
Rechtsfähiger Verein gemäß § 22 BGB
Senckenberganlage 25
60325 Frankfurt
Direktorium: Prof. Dr. Dr. h.c. Volker Mosbrugger, Prof. Dr. Michael Türkay, 
Dr. Johannes Heilmann, Prof. Dr. Pedro Martinez Arbizu, Prof. Dr. Georg Zizka, 
Prof. Dr. Uwe Fritz
Vorsitzender des Präsidiums: Dietmar Schmid
Aufsichtsbehörde: Magistrat der Stadt Frankfurt am Main (Ordnungsamt)

__
R-help@r-project.org mailing list
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.


Re: [R] string substitution for argument in function

2012-03-26 Thread R. Michael Weylandt
'elem' is an attribute (the name) of the 'data' variable (bad name for
a variable), not something that is independently accessible. By very
(exceptionally!) loose analogy, think of it as being something like a
field/property of an object -- you can't get to it directly (and
that's a good thing) but you can get to it by way of its object.

Internally, it is a string, just not printed as such because its
serving a different purpose than being a string datum.

Hope this helps,
Michael

On Mon, Mar 26, 2012 at 11:31 AM, Prof. Dr. Pedro Martinez Arbizu
pmarti...@senckenberg.de wrote:
 Me again,
 what I really dont understand is the behaivour of R here.
 notice, elem is not written in brackets, why it is interpreted as string
 and not as a variable?
 I would expect an error elem not found because it is not defined as
 variable, but in contrary it is accepted as column name, see below:


 a = c('one','two','three')
 data =c()
 data=cbind(elem = 2,data)
 data
     elem
 [1,]    2

 data=cbind('elem' = 2,data)
 data
     elem elem
 [1,]    2    2

 elem
 Fehler: Objekt 'elem' nicht gefunden

 pedro









 On 26.03.2012 17:22, Prof. Dr. Pedro Martinez Arbizu wrote:

 HI, thanks Weidong, Henrique,
 this works for the example (may be a bad example, but it should be as
 simple as possible),

 This is however only a workaround to name the columns of a dataframe, but
 is not addressing the problem itself, that is, how can I substitute a string
 (used as argument in a function) within a function call in a for-loop??

 I dont know if I have explain myself,

 something like this works well:

  a = c('one','two','three')

  data= c()
  for(elem in a){data=cbind('title' = elem,data)}
  data

     title   title title
 [1,] three two one


 So I can use 'title' = elem within the function and it will substitute
 elem by a name, every loop by a different name of a list.

 ...but I cannot use the contrary:   elem = 'something' within the function
 call.

 My real life problem is using randomForest() I want to balance the class
 frequency with the argument sampsize = c( 'absent' = 23, elem = 23)
 where elem is everytime a different name taken from a list in a for-loop.

 Regards Pedro



 On 26.03.2012 02:17, Weidong Gu wrote:

 Hi,

 This may help

  a = c('one','two','three')
 data.frame(eval(substitute(rbind(var,2),list(var=a

 ?substitute
 ?eval

 Weidong Gu


 On Sun, Mar 25, 2012 at 5:22 PM, Pedro Martinez
 pedro.marti...@senckenberg.de  wrote:

 hello,
 I want to iterate through a list of names and use each element as an
 argument in a function. For instance:

 a = c('one','two','three')
 data= c()
 for(elem in a){data=cbind(elem = 2,data)}
 data

     elem elem elem
 [1,]    2    2    2

 instead I want 'elem' to be substituted by the string in the list. Doing
 it by hand would be:

 data = cbind('one'=2,data)
 data = cbind('two'=2,data)
 data = cbind('three'=2,data)
 data

     'one' 'two' 'three'
 [1,]    2    2    2

 I guess that the clue would be in sub(),gsub(), paste() or similar but I
 didnt get it to work.

 I am comming from python were we woudl do something like:

 a = ['one','two','three']
 data = {}
 for elem in a:
       data[elem] = 2
 data

 {'three': 2, 'two': 2, 'one': 2}

 Thanks, Pedro


 -
 Prof. Dr. P. Martinez Arbizu
 DZMB-Forschungsinstitut Senckenberg

 Suedstrand 44
 D-26382 Wilhelmshaven
 Germany

 Tel: +49 (0)4421 9475-100
 Fax: +49 (0)4421 9475-111

 Email: pmarti...@senckenberg.de

 Senckenberg Gesellschaft für Naturforschung
 Rechtsfähiger Verein gemäß § 22 BGB
 Senckenberganlage 25
 60325 Frankfurt
 Direktorium: Prof. Dr. Dr. h.c. Volker Mosbrugger, Prof. Dr. Michael
 Türkay, Dr. Johannes Heilmann, Prof. Dr. Pedro Martinez Arbizu, Prof.
 Dr. Georg Zizka, Prof. Dr. Uwe Fritz
 Vorsitzender des Präsidiums: Dietmar Schmid
 Aufsichtsbehörde: Magistrat der Stadt Frankfurt am Main (Ordnungsamt)

 __
 R-help@r-project.org mailing list
 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.





 --
 -
 Prof. Dr. Pedro Martinez Arbizu

 Deutsches Zentrum für Marine Biodiversitätsforschung
 Senckenberg am Meer
 Südstrand 44
 26382 Wilhelmshaven
 Tel +49 (0)4421 9475 100
 Fax +49 (0)4421 9475 111
 Email: pmarti...@senckenberg.de
 http://www.senckenberg.de/root/index.php?page_id=2141

 und

 C.v.O. Universität Oldenburg
 FK V, Institut für Biologie u. Umweltwissenschaften
 Ag Marine Biodiversität

 **
 Senckenberg Gesellschaft für Naturforschung
 Rechtsfähiger Verein gemäß § 22 BGB
 Senckenberganlage 25
 60325 Frankfurt
 Direktorium: Prof. Dr. Dr. h.c. Volker Mosbrugger, Prof. Dr. Michael Türkay,
 Dr. Johannes Heilmann, Prof. Dr. Pedro Martinez Arbizu, Prof. Dr. Georg
 Zizka, Prof. Dr. Uwe Fritz
 Vorsitzender des 

[R] string substitution for argument in function

2012-03-25 Thread Pedro Martinez
hello,
I want to iterate through a list of names and use each element as an
argument in a function. For instance:

 a = c('one','two','three')
 data= c()
 for(elem in a){data=cbind(elem = 2,data)}
 data
 elem elem elem
[1,]222

instead I want 'elem' to be substituted by the string in the list. Doing
it by hand would be:
 data = cbind('one'=2,data)
 data = cbind('two'=2,data)
 data = cbind('three'=2,data)
 data
 'one' 'two' 'three'
[1,]222

I guess that the clue would be in sub(),gsub(), paste() or similar but I
didnt get it to work.

I am comming from python were we woudl do something like:
 a = ['one','two','three']
 data = {}

 for elem in a:
   data[elem] = 2
 
 data
{'three': 2, 'two': 2, 'one': 2}

Thanks, Pedro


-
Prof. Dr. P. Martinez Arbizu
DZMB-Forschungsinstitut Senckenberg

Suedstrand 44
D-26382 Wilhelmshaven
Germany

Tel: +49 (0)4421 9475-100
Fax: +49 (0)4421 9475-111

Email: pmarti...@senckenberg.de

Senckenberg Gesellschaft für Naturforschung
Rechtsfähiger Verein gemäß § 22 BGB
Senckenberganlage 25
60325 Frankfurt
Direktorium: Prof. Dr. Dr. h.c. Volker Mosbrugger, Prof. Dr. Michael
Türkay, Dr. Johannes Heilmann, Prof. Dr. Pedro Martinez Arbizu, Prof.
Dr. Georg Zizka, Prof. Dr. Uwe Fritz
Vorsitzender des Präsidiums: Dietmar Schmid
Aufsichtsbehörde: Magistrat der Stadt Frankfurt am Main (Ordnungsamt)

__
R-help@r-project.org mailing list
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.


Re: [R] string substitution for argument in function

2012-03-25 Thread Weidong Gu
Hi,

This may help

 a = c('one','two','three')
data.frame(eval(substitute(rbind(var,2),list(var=a

?substitute
?eval

Weidong Gu


On Sun, Mar 25, 2012 at 5:22 PM, Pedro Martinez
pedro.marti...@senckenberg.de wrote:
 hello,
 I want to iterate through a list of names and use each element as an
 argument in a function. For instance:

 a = c('one','two','three')
 data= c()
 for(elem in a){data=cbind(elem = 2,data)}
 data
     elem elem elem
 [1,]    2    2    2

 instead I want 'elem' to be substituted by the string in the list. Doing
 it by hand would be:
 data = cbind('one'=2,data)
 data = cbind('two'=2,data)
 data = cbind('three'=2,data)
 data
     'one' 'two' 'three'
 [1,]    2    2    2

 I guess that the clue would be in sub(),gsub(), paste() or similar but I
 didnt get it to work.

 I am comming from python were we woudl do something like:
 a = ['one','two','three']
 data = {}

 for elem in a:
       data[elem] = 2

 data
 {'three': 2, 'two': 2, 'one': 2}

 Thanks, Pedro


 -
 Prof. Dr. P. Martinez Arbizu
 DZMB-Forschungsinstitut Senckenberg

 Suedstrand 44
 D-26382 Wilhelmshaven
 Germany

 Tel: +49 (0)4421 9475-100
 Fax: +49 (0)4421 9475-111

 Email: pmarti...@senckenberg.de

 Senckenberg Gesellschaft für Naturforschung
 Rechtsfähiger Verein gemäß § 22 BGB
 Senckenberganlage 25
 60325 Frankfurt
 Direktorium: Prof. Dr. Dr. h.c. Volker Mosbrugger, Prof. Dr. Michael
 Türkay, Dr. Johannes Heilmann, Prof. Dr. Pedro Martinez Arbizu, Prof.
 Dr. Georg Zizka, Prof. Dr. Uwe Fritz
 Vorsitzender des Präsidiums: Dietmar Schmid
 Aufsichtsbehörde: Magistrat der Stadt Frankfurt am Main (Ordnungsamt)

 __
 R-help@r-project.org mailing list
 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.

__
R-help@r-project.org mailing list
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.


Re: [R] string substitution for argument in function

2012-03-25 Thread Henrique Dallazuanna
try this:

`names-`(rep(2, 3), a)


On Sun, Mar 25, 2012 at 6:22 PM, Pedro Martinez
pedro.marti...@senckenberg.de wrote:

 hello,
 I want to iterate through a list of names and use each element as an
 argument in a function. For instance:

  a = c('one','two','three')
  data= c()
  for(elem in a){data=cbind(elem = 2,data)}
  data
     elem elem elem
 [1,]    2    2    2

 instead I want 'elem' to be substituted by the string in the list. Doing
 it by hand would be:
  data = cbind('one'=2,data)
  data = cbind('two'=2,data)
  data = cbind('three'=2,data)
  data
     'one' 'two' 'three'
 [1,]    2    2    2

 I guess that the clue would be in sub(),gsub(), paste() or similar but I
 didnt get it to work.

 I am comming from python were we woudl do something like:
  a = ['one','two','three']
  data = {}

  for elem in a:
        data[elem] = 2

  data
 {'three': 2, 'two': 2, 'one': 2}

 Thanks, Pedro


 -
 Prof. Dr. P. Martinez Arbizu
 DZMB-Forschungsinstitut Senckenberg

 Suedstrand 44
 D-26382 Wilhelmshaven
 Germany

 Tel: +49 (0)4421 9475-100
 Fax: +49 (0)4421 9475-111

 Email: pmarti...@senckenberg.de

 Senckenberg Gesellschaft für Naturforschung
 Rechtsfähiger Verein gemäß § 22 BGB
 Senckenberganlage 25
 60325 Frankfurt
 Direktorium: Prof. Dr. Dr. h.c. Volker Mosbrugger, Prof. Dr. Michael
 Türkay, Dr. Johannes Heilmann, Prof. Dr. Pedro Martinez Arbizu, Prof.
 Dr. Georg Zizka, Prof. Dr. Uwe Fritz
 Vorsitzender des Präsidiums: Dietmar Schmid
 Aufsichtsbehörde: Magistrat der Stadt Frankfurt am Main (Ordnungsamt)

 __
 R-help@r-project.org mailing list
 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.




--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

__
R-help@r-project.org mailing list
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.


[R] String position character replacement

2012-02-08 Thread Yang, Joy (NIH/NHGRI) [F]
Hi,

Is there a way to efficiently replace specified indices in a string with 
another character? For example, if I had a vector of strings such as

[1] hellohowareyoudoing
[2] imgoodhowareyou
[3] goodandyou
[4] yesimgoodijusttoldyou
[5] ohyesthatsright

and had a list of positions that I want to replace with the character -

[[1]]
[1]  3  9

[[2]]
[1]  3  4

[[3]]
[1]  4  7

[[4]]
[1] 5 6 7 8 9

[[5]]
[1]  2  5  7 12

I would like to get

[1] he-lohow-reyoudoing
[2] im--odhowareyou
[3] goo-an-you
[4] yesi-ijusttoldyou
[5] o-ye-t-atsr-ght

Is there an easy way to do this? Or would the easiest way be writing a function 
to take substrings of the original vector and pasting in the replacement 
character?

Thanks in advance!
Joy
__
R-help@r-project.org mailing list
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.


Re: [R] String position character replacement

2012-02-08 Thread Jorge I Velez
Hi Joy,

Perhaps not the easiest way, but the following seems to work:

x - c(hellohowareyoudoing, imgoodhowareyou, goodandyou,
yesimgoodijusttoldyou, ohyesthatsright)
pos - list(c(3, 9), c(3,4), c(4,7), 5:9, c(2, 5, 7, 12))

sapply(1:length(pos), function(i){
xx - strsplit(x, )[[i]]
xx[pos[[i]]] - -
paste(xx, sep = , collapse = )
})

[1] he-lohow-reyoudoing   im--odhowareyou   goo-an-you
[4] yesi-ijusttoldyou o-ye-t-atsr-ght



On Wed, Feb 8, 2012 at 12:33 PM, Yang, Joy (NIH/NHGRI) [F]  wrote:

 Hi,

 Is there a way to efficiently replace specified indices in a string with
 another character? For example, if I had a vector of strings such as

 [1] hellohowareyoudoing
 [2] imgoodhowareyou
 [3] goodandyou
 [4] yesimgoodijusttoldyou
 [5] ohyesthatsright

 and had a list of positions that I want to replace with the character -

 [[1]]
 [1]  3  9

 [[2]]
 [1]  3  4

 [[3]]
 [1]  4  7

 [[4]]
 [1] 5 6 7 8 9

 [[5]]
 [1]  2  5  7 12

 I would like to get

 [1] he-lohow-reyoudoing
 [2] im--odhowareyou
 [3] goo-an-you
 [4] yesi-ijusttoldyou
 [5] o-ye-t-atsr-ght

 Is there an easy way to do this? Or would the easiest way be writing a
 function to take substrings of the original vector and pasting in the
 replacement character?

 Thanks in advance!
 Joy
 __
 R-help@r-project.org mailing list
 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.


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.


Re: [R] String position character replacement

2012-02-08 Thread Sarah Goslee
And here's an alternative solution:

subchar - function(string, pos, char=-) {
for(i in pos) {
string - gsub(paste(^(.{, i-1, })., sep=), \\1-, string)
}
string
}


 subchar(hellohowareyoudoing, 3)
[1] he-lohowareyoudoing

 subchar(hellohowareyoudoing, c(3, 9))
[1] he-lohow-reyoudoing

 avec - c(hellohowareyoudoing, imgoodhowareyou, goodandyou, 
 yesimgoodijusttoldyou, ohyesthatsright)
 alist - list(c(3, 9), c(3, 4), c(4, 7), c(5,6,7,8,9), c(2,5,7,12))

 sapply(1:length(avec), function(x)subchar(avec[x], alist[[x]]))
[1] he-lohow-reyoudoing   im--odhowareyou   goo-an-you
[4] yesi-ijusttoldyou o-ye-t-atsr-ght


Sarah


On Wed, Feb 8, 2012 at 12:33 PM, Yang, Joy (NIH/NHGRI) [F]
joy.y...@nih.gov wrote:
 Hi,

 Is there a way to efficiently replace specified indices in a string with 
 another character? For example, if I had a vector of strings such as

 [1] hellohowareyoudoing
 [2] imgoodhowareyou
 [3] goodandyou
 [4] yesimgoodijusttoldyou
 [5] ohyesthatsright

 and had a list of positions that I want to replace with the character -

 [[1]]
 [1]  3  9

 [[2]]
 [1]  3  4

 [[3]]
 [1]  4  7

 [[4]]
 [1] 5 6 7 8 9

 [[5]]
 [1]  2  5  7 12

 I would like to get

 [1] he-lohow-reyoudoing
 [2] im--odhowareyou
 [3] goo-an-you
 [4] yesi-ijusttoldyou
 [5] o-ye-t-atsr-ght

 Is there an easy way to do this? Or would the easiest way be writing a 
 function to take substrings of the original vector and pasting in the 
 replacement character?

 Thanks in advance!
 Joy


-- 
Sarah Goslee
http://www.functionaldiversity.org

__
R-help@r-project.org mailing list
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.


Re: [R] String position character replacement

2012-02-08 Thread Gabor Grothendieck
On Wed, Feb 8, 2012 at 12:33 PM, Yang, Joy (NIH/NHGRI) [F]
joy.y...@nih.gov wrote:
 Hi,

 Is there a way to efficiently replace specified indices in a string with 
 another character? For example, if I had a vector of strings such as

 [1] hellohowareyoudoing
 [2] imgoodhowareyou
 [3] goodandyou
 [4] yesimgoodijusttoldyou
 [5] ohyesthatsright

 and had a list of positions that I want to replace with the character -

 [[1]]
 [1]  3  9

 [[2]]
 [1]  3  4

 [[3]]
 [1]  4  7

 [[4]]
 [1] 5 6 7 8 9

 [[5]]
 [1]  2  5  7 12

 I would like to get

 [1] he-lohow-reyoudoing
 [2] im--odhowareyou
 [3] goo-an-you
 [4] yesi-ijusttoldyou
 [5] o-ye-t-atsr-ght

 Is there an easy way to do this? Or would the easiest way be writing a 
 function to take substrings of the original vector and pasting in the 
 replacement character?


Using this input:

pos - list(c(3, 9), c(3, 4))
s - c(hellohowareyoudoing, imgoodhowareyou)

we can use regmatches like this where the lapply on the LHS constructs
a matches list and the lapply on the RHS constructs a list of -
characters:

f - function(p) structure(p, match.length = rep(1, length(p)))
regmatches(s, lapply(pos, f)) - lapply(pos, function(p) -)

The resulting s is:

 s
[1] he-lohow-reyoudoing im--odhowareyou

-- 
Statistics  Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

__
R-help@r-project.org mailing list
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.


Re: [R] String position character replacement

2012-02-08 Thread Yang, Joy (NIH/NHGRI) [F]
Thank you both! I was working along the lines of Jorge's method, but was taking 
longer than it should. Sarah's is actually a lot faster.

Thanks again,
Joy


From: Sarah Goslee [sarah.gos...@gmail.com]
Sent: Wednesday, February 08, 2012 1:30 PM
To: Yang, Joy (NIH/NHGRI) [F]
Cc: r-help@R-project.org
Subject: Re: [R] String position character replacement

And here's an alternative solution:

subchar - function(string, pos, char=-) {
for(i in pos) {
string - gsub(paste(^(.{, i-1, })., sep=), \\1-, string)
}
string
}


 subchar(hellohowareyoudoing, 3)
[1] he-lohowareyoudoing

 subchar(hellohowareyoudoing, c(3, 9))
[1] he-lohow-reyoudoing

 avec - c(hellohowareyoudoing, imgoodhowareyou, goodandyou, 
 yesimgoodijusttoldyou, ohyesthatsright)
 alist - list(c(3, 9), c(3, 4), c(4, 7), c(5,6,7,8,9), c(2,5,7,12))

 sapply(1:length(avec), function(x)subchar(avec[x], alist[[x]]))
[1] he-lohow-reyoudoing   im--odhowareyou   goo-an-you
[4] yesi-ijusttoldyou o-ye-t-atsr-ght


Sarah


On Wed, Feb 8, 2012 at 12:33 PM, Yang, Joy (NIH/NHGRI) [F]
joy.y...@nih.gov wrote:
 Hi,

 Is there a way to efficiently replace specified indices in a string with 
 another character? For example, if I had a vector of strings such as

 [1] hellohowareyoudoing
 [2] imgoodhowareyou
 [3] goodandyou
 [4] yesimgoodijusttoldyou
 [5] ohyesthatsright

 and had a list of positions that I want to replace with the character -

 [[1]]
 [1]  3  9

 [[2]]
 [1]  3  4

 [[3]]
 [1]  4  7

 [[4]]
 [1] 5 6 7 8 9

 [[5]]
 [1]  2  5  7 12

 I would like to get

 [1] he-lohow-reyoudoing
 [2] im--odhowareyou
 [3] goo-an-you
 [4] yesi-ijusttoldyou
 [5] o-ye-t-atsr-ght

 Is there an easy way to do this? Or would the easiest way be writing a 
 function to take substrings of the original vector and pasting in the 
 replacement character?

 Thanks in advance!
 Joy


--
Sarah Goslee
http://www.functionaldiversity.org

__
R-help@r-project.org mailing list
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.


Re: [R] String position character replacement

2012-02-08 Thread Petr Savicky
On Wed, Feb 08, 2012 at 01:30:55PM -0500, Sarah Goslee wrote:
 And here's an alternative solution:
 
 subchar - function(string, pos, char=-) {
   for(i in pos) {
   string - gsub(paste(^(.{, i-1, })., sep=), \\1-, string)
   }
   string
 }

Hi.

Try the following modification.

  subchar2 - function(string, pos) {
for(i in pos) {
substr(string, i, i) - -
}
string
  }

  avec - c(hellohowareyoudoing, imgoodhowareyou, goodandyou, 
yesimgoodijusttoldyou, ohyesthatsright)
  alist - list(c(3, 9), c(3, 4), c(4, 7), c(5,6,7,8,9), c(2,5,7,12))
  sapply(1:length(avec), function(x) subchar2(avec[x], alist[[x]]))

  [1] he-lohow-reyoudoing   im--odhowareyou   goo-an-you   
  [4] yesi-ijusttoldyou o-ye-t-atsr-ght  

Hope this helps.

Petr Savicky.

__
R-help@r-project.org mailing list
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.


Re: [R] String position character replacement

2012-02-08 Thread Henrique Dallazuanna
Try this:

sapply(mapply(replace, x = strsplit(avec, NULL), list = alist, MoreArgs =
list(values = -)), paste, collapse = )

On Wed, Feb 8, 2012 at 3:33 PM, Yang, Joy (NIH/NHGRI) [F]
joy.y...@nih.govwrote:

 Hi,

 Is there a way to efficiently replace specified indices in a string with
 another character? For example, if I had a vector of strings such as

 [1] hellohowareyoudoing
 [2] imgoodhowareyou
 [3] goodandyou
 [4] yesimgoodijusttoldyou
 [5] ohyesthatsright

 and had a list of positions that I want to replace with the character -

 [[1]]
 [1]  3  9

 [[2]]
 [1]  3  4

 [[3]]
 [1]  4  7

 [[4]]
 [1] 5 6 7 8 9

 [[5]]
 [1]  2  5  7 12

 I would like to get

 [1] he-lohow-reyoudoing
 [2] im--odhowareyou
 [3] goo-an-you
 [4] yesi-ijusttoldyou
 [5] o-ye-t-atsr-ght

 Is there an easy way to do this? Or would the easiest way be writing a
 function to take substrings of the original vector and pasting in the
 replacement character?

 Thanks in advance!
 Joy
 __
 R-help@r-project.org mailing list
 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.




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.


Re: [R] String position character replacement

2012-02-08 Thread Yang, Joy (NIH/NHGRI) [F]
Oh cool! I didn't realize you could assign a different value to a substring 
like that.

Thanks!
Joy

From: Petr Savicky [savi...@cs.cas.cz]
Sent: Wednesday, February 08, 2012 3:26 PM
To: r-help@r-project.org
Subject: Re: [R] String position character replacement

On Wed, Feb 08, 2012 at 01:30:55PM -0500, Sarah Goslee wrote:
 And here's an alternative solution:

 subchar - function(string, pos, char=-) {
   for(i in pos) {
   string - gsub(paste(^(.{, i-1, })., sep=), \\1-, string)
   }
   string
 }

Hi.

Try the following modification.

  subchar2 - function(string, pos) {
for(i in pos) {
substr(string, i, i) - -
}
string
  }

  avec - c(hellohowareyoudoing, imgoodhowareyou, goodandyou, 
yesimgoodijusttoldyou, ohyesthatsright)
  alist - list(c(3, 9), c(3, 4), c(4, 7), c(5,6,7,8,9), c(2,5,7,12))
  sapply(1:length(avec), function(x) subchar2(avec[x], alist[[x]]))

  [1] he-lohow-reyoudoing   im--odhowareyou   goo-an-you
  [4] yesi-ijusttoldyou o-ye-t-atsr-ght

Hope this helps.

Petr Savicky.

__
R-help@r-project.org mailing list
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.

__
R-help@r-project.org mailing list
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.


[R] string to list()

2011-11-14 Thread Kevin Burton
I can get an array of strings for the data that I want using 'paste()' as
follows:

 

paste('ma', 1:am$arma[2], '=', coef(am)[1:am$arma[2] + am$arma[1]], sep='')

 

This results in a vector of strings like:

 

[1] ma1=1.17760133668255  ma2=0.649795570407939 ma3=0.329456750858276

 

What I would like is

 

fixed.pars -
list(ma1=1.17760133668255,ma2=0.649795570407939,ma3=0.329456750858276)

 

Is there an 'R' guru that would be willing to suggest a good way of doing
this?

 

Thank you.

 

Kevin


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.


Re: [R] string to list()

2011-11-14 Thread Bert Gunter
Do it in 2 steps:

z - as.list( coef(am)[1:am$arma[2] + am$arma[1]])
names(z) - paste(ma,seq_along(z), sep=)

-- Bert

On Mon, Nov 14, 2011 at 10:40 AM, Kevin Burton rkevinbur...@charter.netwrote:

 I can get an array of strings for the data that I want using 'paste()' as
 follows:



 paste('ma', 1:am$arma[2], '=', coef(am)[1:am$arma[2] + am$arma[1]], sep='')



 This results in a vector of strings like:



 [1] ma1=1.17760133668255  ma2=0.649795570407939 ma3=0.329456750858276



 What I would like is



 fixed.pars -
 list(ma1=1.17760133668255,ma2=0.649795570407939,ma3=0.329456750858276)



 Is there an 'R' guru that would be willing to suggest a good way of doing
 this?



 Thank you.



 Kevin


[[alternative HTML version deleted]]

 __
 R-help@r-project.org mailing list
 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.




-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.


Re: [R] String manipulation with regexpr, got to be a better way

2011-09-30 Thread Eik Vettorazzi
Hi Chris,
why not using routines for dates
dates - c(09/10/2003, 10/22/2005)
format(strptime(dates,format=%m/%d/%Y),%Y)

or take just the last 4 chars from dates
gsub(.*([0-9]{4})$,\\1,dates)

cheers

Am 29.09.2011 16:23, schrieb Chris Conner:
 Help-Rs,
  
 I'm doing some string manipulation in a file where I converted a string date 
 in mm/dd/ format and returned the date .
  
 I've used regexpr (hat tip to Gabor G for a very nice earlier post on this 
 function) in steps (I've un-nested the code and provided it and an example of 
 what I did below.  My question is: is there a more efficient way to do this.  
 Specifically is there a way to use regexpr or some other string function to 
 return not the first instance, but the 2nd (or for that matter 3rd, 4th or 
 5th instance) of a certain string?
  
  #first find the first occurence of / and create a variable for this 
 firstslash - unlist(regexpr(/, dates, fixed = TRUE)) #then use frist/ to 
 cut the string field into an intermediate variable e.g., from 1/1/2008 to 
 1/2008. step1 - substr( dates,  (firstslash + 1), nchar(dates) ) #then 
 repeat steps 1 and 2...there's got to be a better way step2 - 
 unlist(regexpr(/, step1, fixed = TRUE)) #then use step2 to cut string into 
 final product e.g., from 1/2008 to 2008. final - substring(step1,step2 + 1, 
 nchar(step1) )
  
 Thx!
 C
   [[alternative HTML version deleted]]
 
 
 
 
 __
 R-help@r-project.org mailing list
 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.


-- 
Eik Vettorazzi
Institut für Medizinische Biometrie und Epidemiologie
Universitätsklinikum Hamburg-Eppendorf

Martinistr. 52
20246 Hamburg

T ++49/40/7410-58243
F ++49/40/7410-57790

--
Pflichtangaben gemäß Gesetz über elektronische Handelsregister und 
Genossenschaftsregister sowie das Unternehmensregister (EHUG):

Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen Rechts; 
Gerichtsstand: Hamburg

Vorstandsmitglieder: Prof. Dr. Jörg F. Debatin (Vorsitzender), Dr. Alexander 
Kirstein, Joachim Prölß, Prof. Dr. Dr. Uwe Koch-Gromus 

__
R-help@r-project.org mailing list
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.


[R] String manipulation with regexpr, got to be a better way

2011-09-29 Thread Chris Conner
Help-Rs,
 
I'm doing some string manipulation in a file where I converted a string date in 
mm/dd/ format and returned the date .
 
I've used regexpr (hat tip to Gabor G for a very nice earlier post on this 
function) in steps (I've un-nested the code and provided it and an example of 
what I did below.  My question is: is there a more efficient way to do this.  
Specifically is there a way to use regexpr or some other string function to 
return not the first instance, but the 2nd (or for that matter 3rd, 4th or 5th 
instance) of a certain string?
 
 #first find the first occurence of / and create a variable for this 
firstslash - unlist(regexpr(/, dates, fixed = TRUE)) #then use frist/ to cut 
the string field into an intermediate variable e.g., from 1/1/2008 to 1/2008. 
step1 - substr( dates,  (firstslash + 1), nchar(dates) ) #then repeat steps 1 
and 2...there's got to be a better way step2 - unlist(regexpr(/, step1, 
fixed = TRUE)) #then use step2 to cut string into final product e.g., from 
1/2008 to 2008. final - substring(step1,step2 + 1, nchar(step1) )
 
Thx!
C
[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.


Re: [R] String manipulation with regexpr, got to be a better way

2011-09-29 Thread Jean V Adams
Chris Conner wrote on 09/29/2011 09:23:02 AM:
 
 Help-Rs,
  
 I'm doing some string manipulation in a file where I converted a 
 string date in mm/dd/ format and returned the date .
  
 I've used regexpr (hat tip to Gabor G for a very nice earlier post 
 on this function) in steps (I've un-nested the code and provided it 
 and an example of what I did below.  My question is: is there a more
 efficient way to do this.  Specifically is there a way to use 
 regexpr or some other string function to return not the first 
 instance, but the 2nd (or for that matter 3rd, 4th or 5th instance) 
 of a certain string?
  
  #first find the first occurence of / and create a variable for 
 this firstslash - unlist(regexpr(/, dates, fixed = TRUE)) #then 
 use frist/ to cut the string field into an intermediate variable 
 e.g., from 1/1/2008 to 1/2008. step1 - substr( dates,  (firstslash 
 + 1), nchar(dates) ) #then repeat steps 1 and 2...there's got to be 
 a better way step2 - unlist(regexpr(/, step1, fixed = TRUE)) 
 #then use step2 to cut string into final product e.g., from 1/2008 
 to 2008. final - substring(step1,step2 + 1, nchar(step1) )
  
 Thx!
 C


# a couple example dates
dates - c(09/10/2003, 10/22/2005)

# split the dates
dates.split - strsplit(dates, /)

# extract the years
sapply(dates.split, [, 3)

Jean
[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.


Re: [R] string manipulation

2011-08-26 Thread Janko Thyson

You might want to take a look at 'regexpr' and/or 'gregexpr':

mytext - I want the number 2000, not the number two thousand
idx - regexpr(\\d{4}, mytext)
idx - c(idx, (idx + attributes(idx)$match.length)-1)
substr(start=idx[1], stop=idx[2], mytext)

HTH,
Janko

On 26.08.2011 03:51, Lorenzo Cattarino wrote:

Apologies for confusion. What I meant was the following:

mytext- I want the number 2000, not the number two thousand

and the problem is to select 2000 as the first four digits after the word 
number. The position of 2000 in the string might change.

thanks
Lorenzo

-Original Message-
From: Steven Kennedy [mailto:stevenkennedy2...@gmail.com]
Sent: Friday, 26 August 2011 11:31 AM
To: Henrique Dallazuanna
Cc: Lorenzo Cattarino; r-help@r-project.org
Subject: Re: [R] string manipulation

You can split your string, and then only take the first 4 digits after
that (this is only an improvement if your numbers might not be at the
end of mytext):

mytext- I do not want the first number 1234, but the second number 5678
sstr-strsplit(mytext,split=second number )[[1]][2]
nynumbers-substr(sstr,1,4)


On Fri, Aug 26, 2011 at 11:18 AM, Henrique Dallazuannawww...@gmail.com  wrote:

Try this:

gsub(.*second number , , mytext)

On Thu, Aug 25, 2011 at 8:00 PM, Lorenzo Cattarino
l.cattar...@uq.edu.au  wrote:

I R-users,

I am trying to find the way to manipulate a character string to select a 4 
digit number after some specific word/s. Example:

mytext- I do not want the first number 1234, but the second number 5678

Is there any function that allows you to select a certain number of digits (in 
this case 5678) after a particular word/s (e.g., second number)

Thank you for your help

Lorenzo


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.




--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

__
R-help@r-project.org mailing list
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.


__
R-help@r-project.org mailing list
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.



__
R-help@r-project.org mailing list
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.


Re: [R] string manipulation

2011-08-26 Thread Gabor Grothendieck
On Thu, Aug 25, 2011 at 9:51 PM, Lorenzo Cattarino
l.cattar...@uq.edu.au wrote:
 Apologies for confusion. What I meant was the following:

 mytext - I want the number 2000, not the number two thousand

 and the problem is to select 2000 as the first four digits after the word 
 number. The position of 2000 in the string might change.

 thanks
 Lorenzo


strapply in gsubfn searches mytext for the indicated regular
expression and passes the back referenced portion (i.e. the portion of
mytext matching the parenthesized portion of the regular expression)
to the as.numeric function whose output is returned.

library(gsubfn)
strapply(mytext, number.*([0-9]{4}), as.numeric, simplify = TRUE) # 2000

See http://gsubfn.googlecode.com for more info.

-- 
Statistics  Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

__
R-help@r-project.org mailing list
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.


Re: [R] string manipulation

2011-08-26 Thread Jeff Newmiller
.* is greedy... might want regex number[^0-9]*([0-9] {4}) to avoid getting 
1999 from I want the number 2000, not the number 1999.
---
Jeff Newmiller The . . Go Live...
DCN:jdnew...@dcn.davis.ca.us Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

Gabor Grothendieck ggrothendi...@gmail.com wrote:

On Thu, Aug 25, 2011 at 9:51 PM, Lorenzo Cattarino
l.cattar...@uq.edu.au wrote:
 Apologies for confusion. What I meant was the following:

 mytext - I want the number 2000, not the number two thousand

 and the problem is to select 2000 as the first four digits after the word 
 number. The position of 2000 in the string might change.

 thanks
 Lorenzo


strapply in gsubfn searches mytext for the indicated regular
expression and passes the back referenced portion (i.e. the portion of
mytext matching the parenthesized portion of the regular expression)
to the as.numeric function whose output is returned.

library(gsubfn)
strapply(mytext, number.*([0-9]{4}), as.numeric, simplify = TRUE) # 2000

See http://gsubfn.googlecode.com for more info.

-- 
Statistics  Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

_

R-help@r-project.org mailing list
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.


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.


Re: [R] string manipulation

2011-08-26 Thread Gabor Grothendieck
On Fri, Aug 26, 2011 at 7:27 AM, Jeff Newmiller
jdnew...@dcn.davis.ca.us wrote:
 .* is greedy... might want regex number[^0-9]*([0-9] {4}) to avoid
 getting 1999 from I want the number 2000, not the number 1999.

If such inputs are possible we could also do this where we have added
a ? after the * to make the repetition non-greedy and also have used
simplify=unlist and ended it with [1] to get only the first match
since it will otherwise match and return all occurrences:

strapply(mytext, number.*?([0-9]{4}), as.numeric, simplify = unlist)[1] # 2000

-- 
Statistics  Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

__
R-help@r-project.org mailing list
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.


[R] string manipulation

2011-08-25 Thread Lorenzo Cattarino
I R-users,

I am trying to find the way to manipulate a character string to select a 4 
digit number after some specific word/s. Example:

mytext - I do not want the first number 1234, but the second number 5678

Is there any function that allows you to select a certain number of digits (in 
this case 5678) after a particular word/s (e.g., second number)

Thank you for your help

Lorenzo


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.


Re: [R] string manipulation

2011-08-25 Thread Henrique Dallazuanna
Try this:

gsub(.*second number , , mytext)

On Thu, Aug 25, 2011 at 8:00 PM, Lorenzo Cattarino
l.cattar...@uq.edu.au wrote:
 I R-users,

 I am trying to find the way to manipulate a character string to select a 4 
 digit number after some specific word/s. Example:

 mytext - I do not want the first number 1234, but the second number 5678

 Is there any function that allows you to select a certain number of digits 
 (in this case 5678) after a particular word/s (e.g., second number)

 Thank you for your help

 Lorenzo


        [[alternative HTML version deleted]]

 __
 R-help@r-project.org mailing list
 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.




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

__
R-help@r-project.org mailing list
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.


Re: [R] string manipulation

2011-08-25 Thread Steven Kennedy
You can split your string, and then only take the first 4 digits after
that (this is only an improvement if your numbers might not be at the
end of mytext):

mytext - I do not want the first number 1234, but the second number 5678
sstr-strsplit(mytext,split=second number )[[1]][2]
nynumbers-substr(sstr,1,4)


On Fri, Aug 26, 2011 at 11:18 AM, Henrique Dallazuanna www...@gmail.com wrote:
 Try this:

 gsub(.*second number , , mytext)

 On Thu, Aug 25, 2011 at 8:00 PM, Lorenzo Cattarino
 l.cattar...@uq.edu.au wrote:
 I R-users,

 I am trying to find the way to manipulate a character string to select a 4 
 digit number after some specific word/s. Example:

 mytext - I do not want the first number 1234, but the second number 5678

 Is there any function that allows you to select a certain number of digits 
 (in this case 5678) after a particular word/s (e.g., second number)

 Thank you for your help

 Lorenzo


        [[alternative HTML version deleted]]

 __
 R-help@r-project.org mailing list
 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.




 --
 Henrique Dallazuanna
 Curitiba-Paraná-Brasil
 25° 25' 40 S 49° 16' 22 O

 __
 R-help@r-project.org mailing list
 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.


__
R-help@r-project.org mailing list
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.


Re: [R] string manipulation

2011-08-25 Thread jim holtman
To be on the safe side in case there are other characters at the end
of the string, use:

 mytext - I do not want the first number 1234, but the second number 
 5678sadfsadffdsa
 # make sure you get 4 digits
 sub(^.*second number[^[0-9]]*([0-9]{4}).*, \\1, mytext)
[1] 5678



On Thu, Aug 25, 2011 at 7:00 PM, Lorenzo Cattarino
l.cattar...@uq.edu.au wrote:
 I R-users,

 I am trying to find the way to manipulate a character string to select a 4 
 digit number after some specific word/s. Example:

 mytext - I do not want the first number 1234, but the second number 5678

 Is there any function that allows you to select a certain number of digits 
 (in this case 5678) after a particular word/s (e.g., second number)

 Thank you for your help

 Lorenzo


        [[alternative HTML version deleted]]

 __
 R-help@r-project.org mailing list
 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.




-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

__
R-help@r-project.org mailing list
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.


Re: [R] string manipulation

2011-08-25 Thread Lorenzo Cattarino
Apologies for confusion. What I meant was the following:

mytext - I want the number 2000, not the number two thousand

and the problem is to select 2000 as the first four digits after the word 
number. The position of 2000 in the string might change.  

thanks 
Lorenzo

-Original Message-
From: Steven Kennedy [mailto:stevenkennedy2...@gmail.com] 
Sent: Friday, 26 August 2011 11:31 AM
To: Henrique Dallazuanna
Cc: Lorenzo Cattarino; r-help@r-project.org
Subject: Re: [R] string manipulation

You can split your string, and then only take the first 4 digits after
that (this is only an improvement if your numbers might not be at the
end of mytext):

mytext - I do not want the first number 1234, but the second number 5678
sstr-strsplit(mytext,split=second number )[[1]][2]
nynumbers-substr(sstr,1,4)


On Fri, Aug 26, 2011 at 11:18 AM, Henrique Dallazuanna www...@gmail.com wrote:
 Try this:

 gsub(.*second number , , mytext)

 On Thu, Aug 25, 2011 at 8:00 PM, Lorenzo Cattarino
 l.cattar...@uq.edu.au wrote:
 I R-users,

 I am trying to find the way to manipulate a character string to select a 4 
 digit number after some specific word/s. Example:

 mytext - I do not want the first number 1234, but the second number 5678

 Is there any function that allows you to select a certain number of digits 
 (in this case 5678) after a particular word/s (e.g., second number)

 Thank you for your help

 Lorenzo


        [[alternative HTML version deleted]]

 __
 R-help@r-project.org mailing list
 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.




 --
 Henrique Dallazuanna
 Curitiba-Paraná-Brasil
 25° 25' 40 S 49° 16' 22 O

 __
 R-help@r-project.org mailing list
 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.


__
R-help@r-project.org mailing list
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.


[R] String manipulation

2011-06-26 Thread Megh Dal
Dear all, I have following kind of character vector:

Vec - c(344426, dwjjsgcj, 123sgdc, aagha123, sdh343asgh, 123jhd51)


Now I want to split each element of this vector according to numeric and string 
element. For example in the 1st element of that vector, there is no string 
element. Therefore I should get a vector of length 2 like c(, 344426) and 
so on.

Can somebody point me how to achieve that in R? Is there any specific function 
for doing that?

Thanks,


__
R-help@r-project.org mailing list
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.


Re: [R] String manipulation

2011-06-26 Thread Gabor Grothendieck
On Sun, Jun 26, 2011 at 10:54 AM, Megh Dal megh700...@yahoo.com wrote:
 Dear all, I have following kind of character vector:

 Vec - c(344426, dwjjsgcj, 123sgdc, aagha123, sdh343asgh, 
 123jhd51)


 Now I want to split each element of this vector according to numeric and 
 string element. For example in the 1st element of that vector, there is no 
 string element. Therefore I should get a vector of length 2 like c(, 
 344426) and so on.

 Can somebody point me how to achieve that in R? Is there any specific 
 function for doing that?


Try this and see the gsubfn home page at http://gsubfn.googlecode.com
for more info:

library(gsubfn)
strapply(Vec, \\d+|\\D+, c)

-- 
Statistics  Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

__
R-help@r-project.org mailing list
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.


Re: [R] String manipulation

2011-06-26 Thread David Winsemius


On Jun 26, 2011, at 10:54 AM, Megh Dal wrote:


Dear all, I have following kind of character vector:

Vec - c(344426, dwjjsgcj, 123sgdc, aagha123, sdh343asgh,  
123jhd51)



Now I want to split each element of this vector according to numeric  
and string element. For example in the 1st element of that vector,  
there is no string element. Therefore I should get a vector of  
length 2 like c(, 344426) and so on.


Can somebody point me how to achieve that in R? Is there any  
specific function for doing that?


?regex
?strsplit

You don't do a very good job of describing your desired output, so  
this is two versions of what I am guessing that to be:


 cbind(lapply(strsplit(Vec, [^0-9]+), paste, collapse=),
+   lapply(strsplit(Vec, [0-9]+), paste, collapse=) )
 [,1] [,2]
[1,] 344426 
[2,]dwjjsgcj
[3,] 123sgdc
[4,] 123aagha
[5,] 343sdhasgh
[6,] 12351  jhd

 data.frame(numbits=unlist(lapply(strsplit(Vec, [^0-9]+), paste,  
collapse=)),
+   alphabits=unlist(lapply(strsplit(Vec, [0-9]+), paste,  
collapse=)) )

  numbits alphabits
1  344426
2  dwjjsgcj
3 123  sgdc
4 123 aagha
5 343   sdhasgh
6   12351   jhd

--
David Winsemius, MD
West Hartford, CT

__
R-help@r-project.org mailing list
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.


Re: [R] String manipulation

2011-06-26 Thread Gabor Grothendieck
On Sun, Jun 26, 2011 at 11:00 AM, Gabor Grothendieck
ggrothendi...@gmail.com wrote:
 On Sun, Jun 26, 2011 at 10:54 AM, Megh Dal megh700...@yahoo.com wrote:
 Dear all, I have following kind of character vector:

 Vec - c(344426, dwjjsgcj, 123sgdc, aagha123, sdh343asgh, 
 123jhd51)


 Now I want to split each element of this vector according to numeric and 
 string element. For example in the 1st element of that vector, there is no 
 string element. Therefore I should get a vector of length 2 like c(, 
 344426) and so on.

 Can somebody point me how to achieve that in R? Is there any specific 
 function for doing that?


 Try this and see the gsubfn home page at http://gsubfn.googlecode.com
 for more info:

 library(gsubfn)
 strapply(Vec, \\d+|\\D+, c)


Also, if what you want is a leading string which begins Vec[[i]]
followed by a numeric (and everything else is to be ignored) try this:

strapply(Vec, ^(\\D*)(\\d*), c)

If the first component must be string and you don't want to limit it
to two try this (ignoring the warnings):

L - strapply(Vec, \\d+|\\D+, c)
lapply(L, function(x) if (length(x) == 0) x else if
(is.na(as.numeric(x[1]))) x else c(, x))

-- 
Statistics  Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

__
R-help@r-project.org mailing list
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.


Re: [R] R string functions

2011-06-15 Thread Daniel Malter
x-'GTTACTGGTACC'
table(strsplit(x,''))

hth,
Daniel


karena wrote:
 
 Hi, 
 
 I have a string GGCCCAATCGCAATTCCAATT
 
 What I want to do is to count the percentage of each letter in the string,
 what string functions can I use to count the number of each letter
 appearing in the string?
 
 For example, the letter A appeared 6 times, letter T appeared 5 times,
 how can I use a string function to get the these number?
 
 thanks,
 
 karena
 

--
View this message in context: 
http://r.789695.n4.nabble.com/R-string-functions-tp3600484p3600568.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
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.


[R] R string functions

2011-06-15 Thread karena
Hi, 

I have a string GGCCCAATCGCAATTCCAATT

What I want to do is to count the percentage of each letter in the string,
what string functions can I use to count the number of each letter appearing
in the string?

For example, the letter A appeared 6 times, letter T appeared 5 times,
how can I use a string function to get the these number?

thanks,

karena

--
View this message in context: 
http://r.789695.n4.nabble.com/R-string-functions-tp3600484p3600484.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
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.


Re: [R] R string functions

2011-06-15 Thread Peter Alspach
Tena koe Karena

Try:

table(strsplit(GGCCCAATCGCAATTCCAATT, ''))

HTH ...

Peter Alspach

 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
 project.org] On Behalf Of karena
 Sent: Thursday, 16 June 2011 8:37 a.m.
 To: r-help@r-project.org
 Subject: [R] R string functions
 
 Hi,
 
 I have a string GGCCCAATCGCAATTCCAATT
 
 What I want to do is to count the percentage of each letter in the
 string,
 what string functions can I use to count the number of each letter
 appearing
 in the string?
 
 For example, the letter A appeared 6 times, letter T appeared 5
 times,
 how can I use a string function to get the these number?
 
 thanks,
 
 karena
 
 --
 View this message in context: http://r.789695.n4.nabble.com/R-string-
 functions-tp3600484p3600484.html
 Sent from the R help mailing list archive at Nabble.com.
 
 __
 R-help@r-project.org mailing list
 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.

The contents of this e-mail are confidential and may be subject to legal 
privilege.
 If you are not the intended recipient you must not use, disseminate, 
distribute or
 reproduce all or any part of this e-mail or attachments.  If you have received 
this
 e-mail in error, please notify the sender and delete all material pertaining 
to this
 e-mail.  Any opinion or views expressed in this e-mail are those of the 
individual
 sender and may not represent those of The New Zealand Institute for Plant and
 Food Research Limited.

__
R-help@r-project.org mailing list
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.


Re: [R] R string functions

2011-06-15 Thread Steve Lianoglou
Hi,

On Wed, Jun 15, 2011 at 4:37 PM, karena dr.jz...@gmail.com wrote:
 Hi,

 I have a string GGCCCAATCGCAATTCCAATT

 What I want to do is to count the percentage of each letter in the string,
 what string functions can I use to count the number of each letter appearing
 in the string?

 For example, the letter A appeared 6 times, letter T appeared 5 times,
 how can I use a string function to get the these number?

The replies you've already received are already helpful ... in
addition to them, though, I'd suggest you check out the Biostrings
package from bioconductor since it looks like you are working with
DNA:

http://www.bioconductor.org/packages/release/bioc/html/Biostrings.html

There are many (many^2) things already implemented in that package
that you will likely want to do with genomic sequences, and done so in
a memory-and-performance efficient manner.

For this particular example:

R library(Biostrings)
R x - DNAString(GGCCCAATCGCAATTCCAATT)
R oligonucleotideFrequency(x, 1)
A C G T
6 7 7 5

## And just for fun:
R oligonucleotideFrequency(x, 2)
AA AC AG AT CA CC CG CT GA GC GG GT TA TC TG TT
 3  0  0  3  3  3  1  0  0  2  5  0  0  2  0  2

Depending on how much genomic/sequence stuff you are planning to do,
it could be worth your while to invest some time looking into various
functionality the Biostrings (and IRanges) package(s) provides for
you.

Hope that helps,
-steve

-- 
Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact

__
R-help@r-project.org mailing list
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.


  1   2   3   >