[R] Losing factor levels when moving variables from one context to another

Michael Rennie mrennie at utm.utoronto.ca
Thu Feb 1 18:13:41 CET 2007


Hi, there

I'm currently trying to figure out how to keep my "factor" levels for a 
variable when moving it from one data frame or matrix to another.

Example below:

vec1<-(rep("10",5))
vec2<-(rep("30",5))
vec3<-(rep("80",5))
vecs<-c(vec1, vec2, vec3)

resp<-rnorm(2,15)

dat<-as.data.frame(cbind(resp, vecs))
dat$vecs<-factor(dat$vecs)
dat

R returns:
                   resp  vecs
1     1.57606068767956   10
2     2.30271782269308   10
3     2.39874788444542   10
4    0.963987738423353   10
5     2.03620782454740   10
6  -0.0706713324725649   30
7     1.49001721222926   30
8     2.00587718501980   30
9    0.450576585429981   30
10    2.87120375367357   30
11    2.25575058079324   80
12    2.03471288724508   80
13    2.67432066972984   80
14    1.74102136279177   80
15    2.29827581276955   80

and now:

newvar<-(rnorm(15,4))
newdat<-as.data.frame(cbind(newvar, dat$vecs))
newdat

R returns:

       newvar V2
1  4.300788  1
2  5.295951  1
3  5.099849  1
4  3.211045  1
5  3.703554  1
6  3.693826  2
7  5.314679  2
8  4.222270  2
9  3.534515  2
10 4.037401  2
11 4.476808  3
12 4.842449  3
13 3.109677  3
14 4.752961  3
15 4.445216  3
 >

I seem to have lost everything I once has associated with "vecs", and it's 
turned my actual values into arbitrary groupings.

I assume this has something to do with the behaviour of factors? Does 
anyone have any suggestions on how to get my original levels, etc., back?

Cheers,

Mike

Michael Rennie
Ph.D. Candidate, University of Toronto at Mississauga
3359 Mississauga Rd. N.
Mississauga, ON  L5L 1C6
Ph: 905-828-5452  Fax: 905-828-3792
www.utm.utoronto.ca/~w3rennie



More information about the R-help mailing list