Skip to content

creating a factor

5 messages · Simon Hosking, Peter Dalgaard, John Fox +2 more

#
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



I am new to R and have been using various manuals and have made many attempts without success
any help appreciated.
thanks,
Simon
#
Simon Hosking <simon.hosking at general.monash.edu.au> writes:
1: 1
2: 1
3: 1
4: 1
5: 1
6: 1
7: 1
8: 2
9: 2
10: 2
11:
Read 10 items
0: 0.56 0.48
1: 0.22 0.59
2: 0.32 0.64
3: 0.26 0.60
4: 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
11:
V1   V2 f
1  0.56 0.48 1
2  0.22 0.59 1
3  0.32 0.64 1
4  0.26 0.60 1
5  0.25 0.38 1
6  0.24 0.45 1
7  0.56 0.67 1
8  0.78 0.97 2
9  0.87 0.79 2
10 0.82 0.85 2
V1               V2         f
 Min.   :0.2200   Min.   :0.3800   1:7
 1st Qu.:0.2525   1st Qu.:0.5075   2:3
 Median :0.4400   Median :0.6200
 Mean   :0.4880   Mean   :0.6420
 3rd Qu.:0.7250   3rd Qu.:0.7600
 Max.   :0.8700   Max.   :0.9700


so what was the problem?? 

I suspect your df was a matrix, not a data frame: just take
as.data.frame first. Otherwise, you'll find that f gets converted to
numeric.
#
Dear Simon,

One doesn't generally use cbind.data.frame() directly, but rather through 
the generic function cbind(). I believe that the following will give you 
what you want:

fac <- factor(c(rep(1,7), rep(2,3)))
cbind(fac, as.data.frame(mat))

where mat is the matrix.

I hope that this helps,
  John
At 08:43 PM 2/3/2004 +1100, Simon Hosking wrote:
-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox at mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox
#
Simon Hosking <simon.hosking at general.monash.edu.au> writes:
factor(rep(1:2, c(7,3)))
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.
[1] 1 1 1 1 1 1 1 2 2 2
Levels: 1 2
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
`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
`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
#
Hallo
On 3 Feb 2004 at 20:43, Simon Hosking wrote:

            
your.f <- factor(rep(c(1,2),c(7,3)))
your.frame<-cbind(your.f,your.matrix)

with your numbers ordered into 10x2 matrix like

cbind(factor(rep(c(1,2),c(7,3))),matrix(rnorm(20),10,2))
or
data.frame(var.f=factor(rep(c(1,2),c(7,3))),matrix(rnorm(20),10,2))
Did you really follow examples from "An Introduction to R" manual?
Cheers Petr
Petr Pikal
petr.pikal at precheza.cz