[R] Issue using tapply

LCOG1 jroll at lcog.org
Mon Jan 25 19:57:04 CET 2010


Hello all, 
   I am trying to use the tapply function to sum some values and change the
column names of the resulting vector.  

I input 
     Emp  Et
1  10565 ACC
2   7515 ADM
3    625 AGF
4   6243 CNS
5  12721 EDU
6   3924 FIN
7  18140 HLH
8   3686 INF
9  15841 MFG
10   243 MIN
11  1864 MNG
12  4664 OSV
13  5496 PRF
14  4988 PUB
15  2166 REC
16  2153 REL
17 16082 RTL
18  3582 TRN
19   757 UTL
20  5818 WHL

and i want to aggregate certain types based on the following matches

    Et     Em
1  AGF AFREMP
2  MIN MINEMP
3  UTL TCPEMP
4  CNS CONEMP
5  MFG MFGEMP
6  WHL WSTEMP
7  RTL RETEMP
8  TRN TCPEMP
9  INF SVCEMP
10 FIN FINEMP
11 REL FINEMP
12 PRF SVCEMP
13 MNG FINEMP
14 ADM SVCEMP
15 EDU SVCEMP
16 HLH SVCEMP
17 REC SVCEMP
18 ACC SVCEMP
19 RST SVCEMP
20 OSV SVCEMP
21 PUB GVTEMP

but the last value in my result is NA when it should be 5818. so my actual
result is 
AFREMP CONEMP FINEMP GVTEMP MFGEMP MINEMP RETEMP SVCEMP TCPEMP WSTEMP 
   625       6243     7941      4988    15841     243    3582     81035     
6575     NA 

I have rechecks to make sure there isnt a simple labeling error but i havent
found anything.  Below is test code.

Et<-c("AGF","MIN","UTL","CNS","MFG","WHL","RTL","TRN","INF","FIN","REL",
"PRF","MNG","ADM","EDU","HLH","REC","ACC","RST","OSV","PUB")

Em<-c("AFREMP","MINEMP","TCPEMP","CONEMP","MFGEMP","WSTEMP","RETEMP","TCPEMP",
"SVCEMP","FINEMP","FINEMP","SVCEMP","FINEMP","SVCEMP","SVCEMP","SVCEMP","SVCEMP",
"SVCEMP","SVCEMP","SVCEMP","GVTEMP")

EtToEm..<-data.frame(Et,Em)

Emp<-c(10565,7515,625,6243,12721,3924,18140,3686,15841,243,1864,4664,5496,4988
,2166,2153,16082,3582,757,5818)

Et.t<-c("ACC","ADM","AGF","CNS","EDU","FIN","HLH","INF","MFG","MIN"
,"MNG","OSV"
,"PRF","PUB","REC","REL","RTL","TRN","UTL","WHL")

Emp.Et<-data.frame(Emp,Et.t)
names(Emp.Et)[2]<-"Et"

Emp.Et.Em<-as.vector(Emp.Et$Em)
names(Emp.Et.Em) <- Emp.Et$Et

EmpEt.Em <- tapply(Emp.Et.Em[EtToEm..$Et], EtToEm..$Em, sum)

-- 
View this message in context: http://n4.nabble.com/Issue-using-tapply-tp1289681p1289681.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list