Re: [R] Create New variable

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Sun 11 Sep 2005 - 23:25:02 EST

Marc Bernard <bernarduse1@yahoo.fr> writes:

> Dear All,
>
> I want to create one numeric variable from a factor one. here is a simple example:
>
> x1 x2 factor
> x11 x21 f1
> x12 x22 f1
> x13 x23 f2
> x14 x24 f3
> . . .
> . . .
>
> Suppose that the variable factor has 3 levels (f1,f2,f3). I want to
> add to this data frame a numeric variable x4 in (v1,v2,v3) such that
> x4=v1 if factor = f1, x4 = v2 if factor = f2 and x4 = v3 if factor =
> f3.

Just use the factor as an index:

   x4 <- c(v1, v2, v3)[factor]

-- 

   O__  ---- Peter Dalgaard             ุster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)                  FAX: (+45) 35327907

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Sun Sep 11 23:31:09 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 16:50:03 EST