[R] Odp: extracting parts of words or extraxting letter to use in ifelse-func.
petr.pikal at precheza.cz
Fri Feb 13 12:02:40 CET 2009
r-help-bounces at r-project.org napsal dne 13.02.2009 10:38:31:
> I want to make some variables with the ifelse-function, but i don't know
> 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
gives you logical vector and you can directly multiply it by 1 to get 1
for TRUE and 0 for FALSE
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.
> 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
> 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?
> R-kode (just run from line 1-22) ;
> View this message in context:
> Sent from the R help mailing list archive at Nabble.com.
> R-help at r-project.org mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help