Hello, I wonder if someone could send me suggestions on how to solve the following problem: I have a vector of an arbitrary size (ex. data<-c(10,10,11,10,12,11,10,12,11,11,10,11)) and use the table function, which gives the following result 10 11 12 5 5 2 that''s fine, but what I would like to do now is: construct new classes based on the number of classes from table, 10 10, 11 11, 12 12, 10 11, 10 12, 11 12. After that I would like to do tabulation on the pairs in data, and positions in pairs should be unimportant: 10 11 should be treated as the same class as 11 10. So the following result should be obtained: 10 10, 11 11, 12 12, 10 11, 10 12, 11 12 1 , 1 , 0 , 2 , 1 , 2 Remeber that it should be possible to do for an arbitrary number of classes. Best regards, Patrik.Waldmann@djingis.se [[alternate HTML version deleted]]
If I understand you correctly, here's a quick and dirty way: ## Simulate some data: x1 <- sample(3, 20, replace=TRUE) x2 <- sample(3, 20, replace=TRUE) x.tab <- table(x1, x2) x.count <- c(diag(x.tab), x.tab[upper.tri(x.tab)] + x.tab[lower.tri(x.tab)]) The first 3 elements of x.count will be (1,1), (2,2) and (3,3), followed by (1,2)/(2,1), (1,3)/(3,1) and (2,3)/(3,2). HTH, Andy> -----Original Message----- > From: Patrik Waldmann [mailto:Patrik.Waldmann at djingis.se] > Sent: Friday, February 28, 2003 6:10 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Tabulating > > > Hello, > > I wonder if someone could send me suggestions on how to solve > the following problem: > > I have a vector of an arbitrary size (ex. > data<-c(10,10,11,10,12,11,10,12,11,11,10,11)) and use the > table function, which gives the following result > 10 11 12 > 5 5 2 > > that's fine, but what I would like to do now is: > > construct new classes based on the number of classes from > table, 10 10, 11 11, 12 12, 10 11, 10 12, 11 12. After that I > would like to do tabulation on the pairs in data, and > positions in pairs should be unimportant: 10 11 should be > treated as the same class as 11 10. > So the following result should be obtained: > 10 10, 11 11, 12 12, 10 11, 10 12, 11 12 > 1 , 1 , 0 , 2 , 1 , 2 > > Remeber that it should be possible to do for an arbitrary > number of classes. > > Best regards, > > Patrik.Waldmann at djingis.se > [[alternate HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > http://www.stat.math.ethz.ch/mailman/listinfo/r-help >------------------------------------------------------------------------------
Hi I think Andy Liaw's suggestion can be modified to handle my interpretation of Patrik's question; try: R> data [1] 10 10 11 10 12 11 10 12 11 11 10 11 R> table(c(data,NA),c(NA,data)) -> x.tab R> c(diag(x.tab), x.tab[upper.tri(x.tab)] + x.tab[lower.tri(x.tab)]) 10 11 12 NA NA NA 1 1 0 5 2 2 R> so the "5" means that there are five "10 11" or "11 10" pairs best rksh> > Hello, > > I wonder if someone could send me suggestions on how to solve the following problem: > > I have a vector of an arbitrary size (ex. data<-c(10,10,11,10,12,11,10,12,11,11,10,11)) and use the table function, which gives the following result > 10 11 12 > 5 5 2 > > that's fine, but what I would like to do now is: > > construct new classes based on the number of classes from table, 10 10, 11 11, 12 12, 10 11, 10 12, 11 12. After that I would like to do tabulation on the pairs in data, and positions in pairs should be unimportant: 10 11 should be treated as the same class as 11 10. > So the following result should be obtained: > 10 10, 11 11, 12 12, 10 11, 10 12, 11 12 > 1 , 1 , 0 , 2 , 1 , 2 > > Remeber that it should be possible to do for an arbitrary number of classes. > > Best regards, > > Patrik.Waldmann at djingis.se-- Robin Hankin, Lecturer, School of Geography and Environmental Science Tamaki Campus Private Bag 92019 Auckland New Zealand r.hankin at auckland.ac.nz tel 0064-9-373-7599 x6820; FAX 0064-9-373-7042
It seems that you are trying to obtain a tabulations of runs on length 2 in your vector. So for data<-c(10,10,11,10,12,11,10,12,11,11,10,11) #define data2 as: data2 <- c(data[-1],NA) # Then, on way to obain the runs is to use table on data and data2: table(data,data2) data2 data 10 11 12 10 1 2 2 11 3 1 0 12 0 2 0 I believe there is also a package written especially to do analyses of runs in data, but offhand, I don't recall the name of it. Anne ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Anne E. York National Marine Mammal Laboratory Seattle WA 98115-0070 USA e-mail: york at ofis450a.akctr.noaa.gov Voice: +1 206-526-4039 Fax: +1 206-526-6615 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From: "Patrik Waldmann" <Patrik.Waldmann at djingis.se> To: <r-help at stat.math.ethz.ch> Date: Fri, 28 Feb 2003 12:09:38 +0100 Subject: [R] Tabulating Hello, I wonder if someone could send me suggestions on how to solve the following problem: I have a vector of an arbitrary size (ex. data<-c(10,10,11,10,12,11,10,12,11,11,10,11)) and use the table function, which gives the following result 10 11 12 5 5 2 that's fine, but what I would like to do now is: construct new classes based on the number of classes from table, 10 10, 11 11, 12 12, 10 11, 10 12, 11 12. After that I would like to do tabulation on the pairs in data, and positions in pairs should be unimportant: 10 11 should be treated as the same class as 11 10. So the following result should be obtained: 10 10, 11 11, 12 12, 10 11, 10 12, 11 12 1 , 1 , 0 , 2 , 1 , 2 Remeber that it should be possible to do for an arbitrary number of classes. Best regards, Patrik.Waldmann at djingis.se