Re: [R] How to best divide table by table
But *please* do learn from Peter Dalgaard's answer that this has nothing to do with tables (Petr, I agree you have helped Serguei, but...) Using vague language is not helpful in such contexts. And hence, calling data frames 'tab1' is probably not a good idea. {Calling them 'data' is not much better though: fortune(dog) Firstly, don't call your matrix 'matrix'. Would you call your dog 'dog'? Anyway, it might clash with the function 'matrix'. -- Barry Rowlingson R-help (October 2004) } Martin Maechler Petr == Petr Pikal [EMAIL PROTECTED] on Mon, 30 Oct 2006 08:12:57 +0100 writes: Petr Hi Assuming the same order in both tables divide only Petr columns you wont and then add column of names. You Petr need to work with data frame, as working with matrices Petr cannot use mixed types columns. Petr tab1-data.frame(letters[1:3], 1:3) Petr tab2-data.frame(letters[1:3], 10) Petr data.frame(tab1[,1],tab1[,-1]/tab2[,-1]) Petr tab1...1. tab11..tab21. 1 a 0.1 2 b 0.2 3 c Petr 0.3 Petr How to set appropriate column names you can find in Petr help page ?data.frame Petr HTH Petr Petr On 27 Oct 2006 at 18:42, Serguei Kaniovski wrote: Petr Date sent: Fri, 27 Oct 2006 18:42:36 +0200 (CEST) Petr From: Serguei Kaniovski [EMAIL PROTECTED] To: Petr r-help@stat.math.ethz.ch Organization: WIFO Subject: Petr [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@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.
Re: [R] How to best divide table by table
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. tab11..tab21. 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 [EMAIL PROTECTED] To: r-help@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@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 [EMAIL PROTECTED] __ R-help@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.
Re: [R] How to best divide table by table
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 __ R-help@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.
Re: [R] How to best divide table by table
Serguei Kaniovski [EMAIL PROTECTED] writes: 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 So these are _not_ tables, but dataframes and you're trying to take the ratio of two character vectors. So don't do that. Instead, tbl1 - as.table(mat1[-1]) rownames(tbl1) - mat1[1,] etc. Or just put column 1 back in if you insist on sticking with data frames. Or cbind(mat[1,],mat1[-1]/mat2[-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 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ R-help@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.
[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@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.
Re: [R] How to best divide table by table
Serguei Kaniovski [EMAIL PROTECTED] 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) t1 FALSE TRUE 5301 620 10 7 3 28 811 20 9219 t1/t2 FALSE TRUE 5 11 6 11 7 11 8 11 9 11 -- 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 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ R-help@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.