[R] problem of tapply function

Massimiliano Tripoli mtripoli at istat.it
Sat Mar 29 14:51:10 CET 2014


Dear All,
I have a vector x (as a factor, see below) for which I'd like to know frequencies for each
level of the factor itself. I generally use "tapply function" for that but it gave to me 
an unespected result as you can see and according my knowledge of R language and if my memory did not fail.

x <-
c(16056,16056,16056,16056,16056,16056,16056,16057,16057,16057,16058,16060
,16060,16060,16061,16061,16062,16062,16062,16062,16062,16063,16064,16065
,16073,16076,16076,16076,16076,16077,16078,16079,16080,16083,16083,16084
,16086,16086,16087,16087,16088,16088,16088,16088,16088,16088,16088,16088
,16089,16089,16089,16089,16090,16090,16090,16090,16092,16092,16092,16092
,16092,16092,16093,16093,16093,16093,16093,16094,16094,16094,16094,16094
,16094,16094,16094,16095,16095,16095,16095,16095,16095,16095,16095,16095
,16095,16095,16095,16095,16095,16095,16096,16096,16096,16096,16096,16097
,16097,16097,16097,16098,16098,16098,16098,16098,16099,16099,16099,16100
,16100,16101,16101,16103,16106,16109,16110,16112,16114,16116,16117,16117
,16119,16119,16120,16121,16121,16121,16122,16122,16122,16122,16122,16123
,16123,16123,16123,16123,16123,16124,16124,16124,16125,16125,16125,16126
,16126,16126,16127,16127,16128,16128,16129,16129,16129,16129,16130,16131
,16131,16131,16131,16131,16131,16132,16132,16133,16133,16133,16133,16134
,16134,16134,16134,16134,16135,16137,16137,16137,16137,16137,16137,16137
,16138,16138,16138,16138,16138,16138,16138,16138,16138,16139,16139,16139
,16139,16139,16140,16140,16140,16140,16140,16142,16142,16143,16143,16143
,16143,16144,16144,16144,16144,16144,16144,16145,16145,16145,16146,16146
,16146,16146,16146,16147,16147,16147,16150,16151,16151,16153,16154,16155
,16155,16155,16155,16157,16158)

x <- factor(x)

tapply(x,x,fun=length)
  [1]  1  1  1  1  1  1  1  2  2  2  3  4  4  4  5  5  6  6  6  6  6  7  8  9 10
 [26] 11 11 11 11 12 13 14 15 16 16 17 18 18 19 19 20 20 20 20 20 20 20 20 21 21
 [51] 21 21 22 22 22 22 23 23 23 23 23 23 24 24 24 24 24 25 25 25 25 25 25 25 25
 [76] 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 27 27 27 27 27 28 28 28 28 29
[101] 29 29 29 29 30 30 30 31 31 32 32 33 34 35 36 37 38 39 40 40 41 41 42 43 43
[126] 43 44 44 44 44 44 45 45 45 45 45 45 46 46 46 47 47 47 48 48 48 49 49 50 50
[151] 51 51 51 51 52 53 53 53 53 53 53 54 54 55 55 55 55 56 56 56 56 56 57 58 58
[176] 58 58 58 58 58 59 59 59 59 59 59 59 59 59 60 60 60 60 60 61 61 61 61 61 62
[201] 62 63 63 63 63 64 64 64 64 64 64 65 65 65 66 66 66 66 66 67 67 67 68 69 69
[226] 70 71 72 72 72 72 73 74

table(x)
x
16056 16057 16058 16060 16061 16062 16063 16064 16065 16073 16076 16077 16078 
    7     3     1     3     2     5     1     1     1     1     4     1     1 
16079 16080 16083 16084 16086 16087 16088 16089 16090 16092 16093 16094 16095 
    1     1     2     1     2     2     8     4     4     6     5     8    15 
16096 16097 16098 16099 16100 16101 16103 16106 16109 16110 16112 16114 16116 
    5     4     5     3     2     2     1     1     1     1     1     1     1 
16117 16119 16120 16121 16122 16123 16124 16125 16126 16127 16128 16129 16130 
    2     2     1     3     5     6     3     3     3     2     2     4     1 
16131 16132 16133 16134 16135 16137 16138 16139 16140 16142 16143 16144 16145 
    6     2     4     5     1     7     9     5     5     2     4     6     3 
16146 16147 16150 16151 16153 16154 16155 16157 16158 
    5     3     1     2     1     1     4     1     1 
> 
# By looking tapply function help why the table's output is different from tapply's output?
# Anyone could help to me, please?
Massimiliano 
-- 
Massimiliano Tripoli 
Collaboratore T.E.R. scado il 31/12/2014 
ISTAT - DCCN - Direzione Centrale della Contabilità Nazionale 
U.O. Contabilità dei flussi di materia del sistema economico - CSA/C 
Via Depretis, 74/B 00184 Roma 
Tel. 06.4673.3132 
E-mail: mtripoli at istat.it 


Precedentemente:
Collaboratore T.E.R. 
Dipartimento         DICS Dipartimento per i conti nazionali e le statistiche economiche
Direzione         DCCN Direzione Centrale della contabilità nazionale
Servizio         FIP Servizio Statistiche della Finanza Pubblica
Ufficio         FIP/E U.O. Conti trimestrali delle amministrazioni pubbliche e conti della sanità
Via Depretis, 74/B 00184 Roma
Tel. 06.4673.3132
E-mail: mtripoli at istat.it



More information about the R-help mailing list