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
creating a factor
5 messages · Simon Hosking, Peter Dalgaard, John Fox +2 more
Simon Hosking <simon.hosking at general.monash.edu.au> writes:
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
f <- factor(scan())
1: 1 2: 1 3: 1 4: 1 5: 1 6: 1 7: 1 8: 2 9: 2 10: 2 11: Read 10 items
df <- read.table(stdin())
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:
cbind(df,f)
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
summary(cbind(df,f))
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.
O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
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:
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
----------------------------------------------------- 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:
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/
Hallo
On 3 Feb 2004 at 20:43, Simon Hosking wrote:
Hi list, I'd like to make a factor with seven 1s and three 2s using the factor() function. That is,
your.f <- factor(rep(c(1,2),c(7,3)))
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
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))
I am new to R and have been using various manuals and have made many
Did you really follow examples from "An Introduction to R" manual?
attempts without success any help appreciated. thanks, Simon
Cheers Petr
______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Petr Pikal petr.pikal at precheza.cz