[R] creating a factor

Robert W. Baer, Ph.D. rbaer at atsu.edu
Tue Feb 3 17:43:57 CET 2004


> Hi list,
> I'd like to make a factor with seven 1s and three 2s using the
> factor() function.
> That is,
>
> 1
> 1
> 1
> 1
> 1
> 1
> 1
> 2
> 2
> 2
>
>
> I will then bind this factor to the matrix below using cbind.data.frame().
>
> 0.56 0.48
> 0.22 0.59
> 0.32 0.64
> 0.26 0.60
> 0.25 0.38
> 0.24 0.45
> 0.56 0.67
> 0.78 0.97
> 0.87 0.79
> 0.82 0.85
_________________
See if this code sequence gives you insight:

 x1=c(0.56,0.22,0.32,0.26,0.25,0.24,0.56,0.78,0.87,0.82)
 x2=c(0.48,0.59,0.64,0.6,0.38,0.45,0.67,0.97,0.79,0.85)
 x=data.frame(cbind(x1,x2))
fac2=as.factor(c("dog","dog","dog","dog","dog","dog","dog","cat","cat","cat"
))
 xf=cbind(fac2,x)
xf
data.class(xf)
data.class(xf$fac2)
levels(xf$fac2)

Rob Baer




More information about the R-help mailing list