[R] Odp: Text in a character vector to indicate "ifelse" argument

Petr PIKAL petr.pikal at precheza.cz
Thu Jan 29 11:09:53 CET 2009


Hi

r-help-bounces at r-project.org napsal dne 29.01.2009 10:25:13:

> 
> Hello
> 
> I have a data set that looks like this; 
> 
> > b2
>           dato         chr                      status           PRRSvac
> PRRSsanVac PRRSsanDk PRRSdk
> 33  2007-12-03 090432                    Rød SPF 
> 34  2007-02-09 090432              Rød SPF+sanDK 
> 35  2002-12-17 090432                 Rød SPF+DK 
> 36  2002-11-27 090432              Rød SPF+sanDK 
> 37  2002-07-23 090432                 Rød SPF+DK 
> 38  2001-08-23 090432                    Rød SPF 
> 39  2000-01-01 090432          SPF-X,  PRRS-neg. 
> 40  1999-05-01 090432           MS-X,  PRRS-neg. 
> 81  2001-08-23 022458                    Rød SPF 
> 82  1999-01-22 022458          SPF-X,  PRRS-neg. 
> 130 2008-10-16 080385 Rød SPF+Myc+Ap2+Nys+DK+Vac 
> 131 2003-03-18 080385     Rød SPF+Myc+Ap2+DK+Vac 
> 132 2002-11-01 080385         Rød SPF+Myc+DK+Vac 
> 133 2002-02-07 080385            Rød SPF+Myc+Vac 
> 134 2000-09-19 080385         MS-X,  PRRS-pos VAC 
> 135 1999-01-22 080385            MS-X,  PRRS-neg 
> 176 2008-10-28 013168 Rød SPF+Myc+Ap2+Nys+DK+Vac 
> 177 2003-05-23 013168     Rød SPF+Myc+Ap2+DK+Vac 
> 178 2002-11-01 013168         Rød SPF+Myc+DK+Vac 
> 179 2001-07-03 013168            Rød SPF+Myc+Vac 
> 180 2000-09-01 013168         MS-X,  PRRS-pos VAC 
> 181 2000-06-02 013168            MS-X,  PRRS-neg 
> 182 2000-04-03 013168     SKM-X,  +Ap2,  PRRS-neg 
> 183 1999-01-22 013168            MS-X,  PRRS-neg 
> 
> Where I have used;
> 
> b2$PRRSvac <- ifelse(b2$status=='PRRS-pos VAC' | b2$status=='Vac',1,0)
> b2$PRRSdk <- ifelse(b2$status=='PRRS-pos DK' | b2$status=='DK',1,0)
> b2$PRRSsanVac <- ifelse(b2$status=='sanVac',1,0)
> b2$PRRSsanDk <- ifelse(b2$status=='sanDK',1,0)
> 
> to creat the last four variables, but it wont work!!! The variable 
status
> has class "character". 

What "wont work!!!" means

> zdrzeni
   sklon    ot   doba    typ     spolf    spol.f
1     35  3.00  70.00  stand  35.stand  35.stand
2     50 20.00   9.50  stand  50.stand  50.stand
3     50  5.00  29.50  stand  50.stand  50.stand
4     50 15.00  13.00  stand  50.stand  50.stand
....

> zdrzeni$v1<-ifelse(zdrzeni$typ=="stand"|zdrzeni$typ=="not", 1,0)
> zdrzeni
   sklon    ot   doba    typ     spolf    spol.f v1
1     35  3.00  70.00  stand  35.stand  35.stand  1
2     50 20.00   9.50  stand  50.stand  50.stand  1
3     50  5.00  29.50  stand  50.stand  50.stand  1
4     50 15.00  13.00  stand  50.stand  50.stand  1
....

obviously "works", so the problem is in your data and your use of "==".

ifelse(x=="abc", 1,0)

means that the result is 1 if x is exactly equivalent to "abc" and 0 if x 
is " abc", "def-abc" or in any other variation. You probably need to use 
grep and regexpr expression for test but it is not my cup of tea.

Regards
Petr


> 
> Can anyone help me?
> 
> -- 
> View this message in context: http://www.nabble.com/Text-in-a-character-
> vector-to-indicate-%22ifelse%22-argument-tp21722983p21722983.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.




More information about the R-help mailing list