[R] re: pooling categories in a table
Michael Friendly
friendly at yorku.ca
Tue Apr 9 15:34:27 CEST 2002
Perhaps I should have given an example.
# 5 column matrix: Day, Time, Station, State, Freq
tv.matrix<-read.table("C:/R/mosaics/tv.dat")
> tv.matrix[1:5,]
V1 V2 V3 V4 V5
1 1 1 1 1 6
2 2 1 1 1 18
3 3 1 1 1 6
4 4 1 1 1 2
5 5 1 1 1 11
>
tv <- array(tv.matrix[,5], dim=c(5,11,5,3))
dimnames(tv) <-
list(c("Monday","Tuesday","Wednesday","Thursday","Friday"),
c("8:00","8:15","8:30","8:45","9:00","9:15","9:30",
"9:45","10:00","10:15","10:30"),
c("A","C","N","F","Other"), c("O","S","P"))
names(dimnames(tv))<-c("Day", "Time", "Station", "State")
Say I want to collapse the 11 time categories to 3. The real problem
is when I have only the 4-way array (or an equivalent table). But
here, I thought I could just collapse the 2nd column to 3 categories:
tv.matrix[,2] <- 1+ as.integer((tv.matrix[,2]-1) /4)
tv2 <- array(tv.matrix[,5],
dim=c(5,3,5,3))
dimnames(tv2) <-
list(c("Monday","Tuesday","Wednesday","Thursday","Friday"),
c("8:00-8:45","9:00-9:45","10:00-10:30"),
c("A","C","N","F","Other"), c("O","S","P"))
names(dimnames(tv2))<-c("Day", "Time", "Station", "State")
But something is wrong, because the margins are not the same:
> margin.table(tv,1)
Day
Monday Tuesday Wednesday Thursday Friday
21271 20486 19304 19779 17275
> margin.table(tv2,1)
Day
Monday Tuesday Wednesday Thursday Friday
996 912 962 898 771
What am I missing?
-Michael
--
Michael Friendly friendly at yorku.ca
York University http://www.math.yorku.ca/SCS/friendly.html
Psychology Department
4700 Keele Street Tel: (416) 736-5115 x66249
Toronto, Ontario, M3J 1P3 Fax: (416) 736-5814
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list