[R] Odp: Odp: extracting parts of words or extraxting letter to use in ifelse-func.
Petr PIKAL
petr.pikal at precheza.cz
Fri Feb 13 13:42:21 CET 2009
Hi
johannes at dsr.life.ku.dk napsal dne 13.02.2009 13:17:06:
> Hi
>
> I tried what you said, but i gives FALSE all the way down! And I think
its
> because there are other words in phrases as well. So if there is 'San'
> in the phrase it has to be TRUE even thoug there are other words before
and
> after 'San' in the phrase.
I do not understand.
> test<-c(" San", "san", "SAN", "San1", "San")
> set.seed(111)
> sam<-sample(test, 40, replace=T)
> sam=="San"
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
FALSE TRUE
[26] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
FALSE FALSE FALSE TRUE
> sum(sam=="San")
[1] 3
>
gives me TRUE whenever item in test vector equals "San"
Did you read what I wrote about ***exact*** match?
>
> Do you know how to do that?
phrase <- "If you want to discover Sun from some phrase you has to use
regular expressions"
> regexpr("Sun", phrase)
[1] 25
attr(,"match.length")
[1] 3
phrase1 <- "If you want to discover blah from some phrase you has to use
regular expressions"
ph <- c(phrase, phrase1)
ph
[1] "If you want to discover Sun from some phrase you has to use regular
expressions"
[2] "If you want to discover blah from some phrase you has to use regular
expressions"
regexpr("Sun", ph)>0
[1] TRUE FALSE
But I am not an expert in regular expressions.
Regards
Petr
>
> -Joe
>
> Petr Pikal wrote:
> >
> > Hi
> >
> > r-help-bounces at r-project.org napsal dne 13.02.2009 10:38:31:
> >
> >>
> >> Hello
> >>
> >> I want to make some variables with the ifelse-function, but i don't
know
> > how
> >> to do it.
> >>
> >> I want to make these five variables;
> >>
> >> b2$PRRSvac <- ifelse(b2$status=='A' | b2$status=='Aa',1,0)
> >> b2$PRRSdk <- ifelse(b2$status=='B' | b2$status=='Bb',1,0)
> >> b2$sanVac <- ifelse(b2$status=='C' | b2$status=='sanAa',1,0)
> >> b2$sanDk <- ifelse(b2$status=='D' | b2$status=='sanBb',1,0)
> >> b2$totalvac <- ifelse(b2$status=='San',1,0)
> >
> > Basically you do not need ifelse
> >
> > b2$status=='San'
> >
> > gives you logical vector and you can directly multiply it by 1 to get
1
> > for TRUE and 0 for FALSE
> >
> > 1*(b2$status=='San')
> >
> > However it is exact match so if in b2$status is
> >
> > " San" or "san" or "SAN" or "San1"
> >
> > it will give 0 for all of these.
> >
> > Regards
> > Petr
> >
> >>
> >> And
> >>
> >> b2$UGT <- ifelse(b2$status=='UGT',1,0)
> >> b2$KOM <- ifelse(b2$status=='KOM',1,0)
> >>
> >> But, as one from this forum told me, it doesn't work because the
words
> > is in
> >> a wrong format or something like that.
> >>
> >> I have attached the text-file i've used, and the R-kode.
> >>
> >> Hope anyone can help me?
> >>
> >> text-file;
> >>
> >
http://www.nabble.com/file/p21993098/allesd%2528uden%2Bf%25C3%25B8r%2529060209.txt
> >
> >> allesd%28uden+f%C3%B8r%29060209.txt
> >>
> >> R-kode (just run from line 1-22) ;
> >>
http://www.nabble.com/file/p21993098/PRRS%2B%2528med%2Ballle%2BSD%2529.r
> >> PRRS+%28med+allle+SD%29.r
> >> --
> >> View this message in context:
> > http://www.nabble.com/extracting-parts-of-words-
> >> or-extraxting-letter-to-use-in-ifelse-func.-tp21993098p21993098.html
> >> Sent from the R help mailing list archive at Nabble.com.
> >>
> >> ______________________________________________
> >> R-help at 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 at 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.
> >
> >
> Quoted from:
>
http://www.nabble.com/extracting-parts-of-words-or-extraxting-letter-to-use-
> in-ifelse-func.-tp21993098p21994462.html
>
More information about the R-help
mailing list