Hi all, how can I divide two tables of the same dimension so that all names are preserved, ie do not become NA? I have "tab1" and "tab2", each having names in the first column. I want "tab3" with the same names and values "tab1/tab2". Thanks, Serguei
Serguei Kaniovski <kaniovsk at wsr.ac.at> writes:> Hi all, > > how can I divide two tables of the same dimension so that all names are > preserved, ie do not become NA? I have "tab1" and "tab2", each having > names in the first column. I want "tab3" with the same names and values > "tab1/tab2".Explain. If tab1 and tab2 have names "in the first column", how do you expect the divide operation to work? Tables normally do preserve row/column names in arithmetic:> t1 <- t2 <- table(airquality$Month,airquality$Temp> 80) > t1FALSE TRUE 5 30 1 6 20 10 7 3 28 8 11 20 9 21 9> t1/t2FALSE TRUE 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
For example, year X1958 X1973 X1981 X1986 X1995 X2004 X2007 1 QMT 12 41 45 54 62 232 255 2 Belgium 2 5 5 5 5 12 12 3 France 4 10 10 10 10 29 29 year X1958 X1973 X1981 X1986 X1995 X2004 X2007 1 QMT 12 41 45 54 62 232 255 2 Belgium 9054483 9741700 9859000 9859000 10137000 10418000 10497000 3 France 45800391 53294898 55419102 56898199 59418699 62177398 63209000 mat1/mat2 produces year X1958 X1973 X1981 X1986 X1995 X2004 X2007 1 NA 1.00e+00 1.00e+00 1.00e+00 1.00e+00 1.00e+00 1.00e+00 1.00e+00 2 NA 2.21e-07 5.13e-07 5.07e-07 5.07e-07 4.93e-07 1.15e-06 1.14e-06 3 NA 8.73e-08 1.88e-07 1.80e-07 1.76e-07 1.68e-07 4.66e-07 4.59e-07 with NA in the first column. Serguei
Hi Assuming the same order in both tables divide only columns you wont and then add column of names. You need to work with data frame, as working with matrices cannot use mixed types columns. tab1<-data.frame(letters[1:3], 1:3) tab2<-data.frame(letters[1:3], 10) data.frame(tab1[,1],tab1[,-1]/tab2[,-1]) tab1...1. tab1....1..tab2....1. 1 a 0.1 2 b 0.2 3 c 0.3 How to set appropriate column names you can find in help page ?data.frame HTH Petr On 27 Oct 2006 at 18:42, Serguei Kaniovski wrote: Date sent: Fri, 27 Oct 2006 18:42:36 +0200 (CEST) From: Serguei Kaniovski <kaniovsk at wsr.ac.at> To: r-help at stat.math.ethz.ch Organization: WIFO Subject: [R] How to best divide table by table> Hi all, > > how can I divide two tables of the same dimension so that all names > are preserved, ie do not become NA? I have "tab1" and "tab2", each > having names in the first column. I want "tab3" with the same names > and values "tab1/tab2". > > Thanks, > Serguei > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html and provide commented, > minimal, self-contained, reproducible code.Petr Pikal petr.pikal at precheza.cz