-4

Mydata is as under.

dat="   salary   ex
1    1856 1799
2    1856 1800
3    1858 1800
4    1858 1801
5    1862 1803
6    1862 1805
7    1862 1810
8    1865 1805
9    1865 1808
10   1865 1815
11   1865 1820
12   1870 1810
13   1870 1830
14   1880 1840
15   1880 1845
16   1880 1851
17   1880 1853
18   1880 1855
19   1885 1850
20   1885 1852
21   1885 1857
22   1885 1860
23   1898 1855
24   1898 1858
25   1898 1861
26   1898 1863
27   1898 1866
28   1898 1867
29   1898 1890
30   1902 1850
31   1902 1853
32   1902 1869
33   1902 1872
34   1902 1873
35   1915 1850
36   1915 1859
37   1915 1863
38   1915 1868
39   1915 1875
40   1915 1898
"

data<-read.table(text=dat,header=TRUE)   

I want to get the result ,the header is salary,the rownames is ex. I only can get the "total column"

rev(table(cut(data[,2],breaks=seq(1795,1905,10),right=F)))    

How can I get the other data by some code,not by hand?
why is there a strange output in my R console ?

options(digits=15)
options(scipen=30)
data$sal.cat <- cut(data$salary, breaks=seq(1855, 1915, by=10), right=FALSE) 
data$sal.cat 
## [1] [1.85e+03,1.86e+03) [1.85e+03,1.86e+03) 

at last ,i solve it myself,

data$sal.cat <- cut(data$salary, breaks=seq(1855, 1915, by=10), right=FALSE,dig.lab=5)

but can set the dig.lab=5 in options,to get the same output with no dig.lab=5 in my cut function?

  enter image description here

showkey
  • 482
  • 42
  • 140
  • 295
  • 11
    Come on. You've been on SO long enough and asked enough R questions to know that this is not a reproducible example.... – A5C1D2H2I1M1N2O1R2T1 Dec 17 '13 at 07:06
  • Also, can you please reformulate your question, I cannot exactly understand what you want to do... generate a table like the one in the screenshot? By the way: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – nico Dec 17 '13 at 07:08

1 Answers1

1
data$sal.cat <- cut(data$salary, breaks=seq(1855, 1915, by=10), right=FALSE) 
data$ex.cat  <- cut(data$ex,     breaks=seq(1855, 1915, by=10), right=FALSE)
cbind( xtabs(~ sal.cat+ex.cat, data=data), 
       total=rowSums(xtabs(~ sal.cat+ex.cat, data=data)))

            [1855,1865) [1865,1875) [1875,1885) [1885,1895)
[1855,1865)           0           0           0           0
[1865,1875)           0           0           0           0
[1875,1885)           1           0           0           0
[1885,1895)           2           0           0           0
[1895,1905)           4           5           0           1
[1905,1915)           0           0           0           0
            [1895,1905) [1905,1915) total
[1855,1865)           0           0     0
[1865,1875)           0           0     0
[1875,1885)           0           0     1
[1885,1895)           0           0     2
[1895,1905)           0           0    10
[1905,1915)           0           0     0

I should mention that there are other approaches possible. There is a CrossTable function in one of hte contributed packages that delivers a SAS/SPSS style crosstabs output, complete with boundaries made of "+" signs. There is a base::proptable function that will turn a contingency table into either cell , row or column proportions. One can get other options with:

library(sos)
findFn("crosstabs")
findFn("crosstabulation")
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • why is there a strange output in my R console ? > options(digits=15) > options(scipen=30) > data$sal.cat 1 [1.85e+03,1.86e+03) [1.85e+03,1.86e+03) – showkey Dec 18 '13 at 02:19
  • The sal.cat field that you are referencing is not defined in your example above, so it is impossible to know how it was derived. But it looks like it is a factor generated by cut(). Please provide the code used to define this field. – datawookie Dec 18 '13 at 03:51
  • @exegetic: It's not? Then what is the first line of code doing? – IRTFM Dec 18 '13 at 04:32
  • @it_is_a_literature: It is certainly possible to coerce the print functions to display 1855 and 1865 as: `[1.85e+03,1.86e+03)`. This does seem entirely tangential to the question, though. The content of levels (which are _not_ numeric) will not be affected by changes settings for how numbers are printed. – IRTFM Dec 18 '13 at 04:36
  • my code is the same as DWin provided ,you can try it and see what happen in your console. – showkey Dec 18 '13 at 05:50
  • please try in your computer ,i have reinstalled R ,it still remains. – showkey Dec 18 '13 at 08:48
  • at last ,i solve it myself, data$sal.cat <- cut(data$salary, breaks=seq(1855, 1915, by=10), right=FALSE,dig.lab=5),but can set the dig.lab=5 in options,to get the same output with no `dig.lab=5` in my cu function? – showkey Dec 18 '13 at 09:08