[R] creating a factor

Douglas Bates bates at stat.wisc.edu
Tue Feb 3 14:10:11 CET 2004


Simon Hosking <simon.hosking at general.monash.edu.au> writes:

> 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

factor(rep(1:2, c(7,3)))

> 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

It is not a good idea to use methods like cbind.data.frame directly.
Use the generic function cbind instead.  The point of having method
functions is to be able to choose the method that is appropriate to
the data.

If you have the matrix shown above stored as a matrix named mat then

cbind(factor(rep(1:2, c(7,3))), mat)

will work but it will also work if mat is a data frame.

> factor(rep(1:2, c(7,3)))
 [1] 1 1 1 1 1 1 1 2 2 2
Levels: 1 2
> mat = read.table("/tmp/foo.dat")
> mat
     V1   V2
1  0.56 0.48
2  0.22 0.59
3  0.32 0.64
4  0.26 0.60
5  0.25 0.38
6  0.24 0.45
7  0.56 0.67
8  0.78 0.97
9  0.87 0.79
10 0.82 0.85
> str(mat)
`data.frame':	10 obs. of  2 variables:
 $ V1: num  0.56 0.22 0.32 0.26 0.25 0.24 0.56 0.78 0.87 0.82
 $ V2: num  0.48 0.59 0.64 0.6 0.38 0.45 0.67 0.97 0.79 0.85
> mm = cbind(factor(rep(1:2, c(7,3))), mat)
> str(mm)
`data.frame':	10 obs. of  3 variables:
 $ factor(rep(1:2, c(7, 3))): Factor w/ 2 levels "1","2": 1 1 1 1 1 1 1 2 2 2
 $ V1                       : num  0.56 0.22 0.32 0.26 0.25 0.24 0.56 0.78 0.87 0.82
 $ V2                       : num  0.48 0.59 0.64 0.6 0.38 0.45 0.67 0.97 0.79 0.85

-- 
Douglas Bates                            bates at stat.wisc.edu
Statistics Department                    608/262-2598
University of Wisconsin - Madison        http://www.stat.wisc.edu/~bates/




More information about the R-help mailing list