[R] Data frame manipulation

LCOG1 jroll at lcog.org
Fri May 28 02:14:22 CEST 2010


Hello All, 
Please consider the following:

TotEmp<-c(19,6,1,1,8,44,2,33,48,1)
ClusterType<-c("AGF","CNS","OSV","RTL","RTL","TRN","REL","ACC_CLUST","RTL","WHL")
Taz<-c(0,0,0,100,100,100,101,101,102,103)

AllCtTypes_<-c("AGF","CNS","OSV","RTL","TRN","REL","ACC_CLUST","WHL","ADM_CLUST",
"HLH","HLH_CLUST","ACC","RTL_CLUST","MFG","ADM","MFG_CLUST","CNS_CLUST","PRF","PUB",
"FIN","INF_CLUST","INF","EDU_CLUST","REC","EDU",
"MNG","UTL","MIN")
#Build data frame
EmpTotCt.Zn..<-data.frame(TotEmp,ClusterType,Taz)
#Reverse rows to columns
EmpTotCt.Zn2..<-as.data.frame(t(as.matrix(EmpTotCt.Zn..)))


"EmpTotCt.Zn.." is a data frame that i would like to alter by adding new
columns and input 0s where no values exist.  I tried the line below as its
the only way i know of switching columns to rows but its far from what i am
looking for.  So "EmpTotCt.Zn.." returns

   TotEmp ClusterType Taz
1      19         AGF       0
2       6         CNS        0
3       1         OSV       0
4       1         RTL     100
5       8         RTL     100
6      44         TRN    100
7       2         REL     101
8      33   ACC_CLUST 101
9      48         RTL     102
10      1         RTL     103

But what i want is to return the below:

	AGF	CNS	OSV	RTL	RTL	TRN	REL	ACC_CLUST	RTL
0	19	6	1	0	0	0	0	0	              0
100	0	0	0	1	8	44	0	0	              0
101	0	0	0	0	0	0	2	33	              0
102	0	0	0	0	0	0	0	0	             48
103	0	0	0	0	0	0	0	0	               1

Where the rows represent "Taz" and the columns represent ALL "ClusterType"'s
found in "AllCtTypes_", this would mean that the above output example would
have many more columns with 0s in all the rows since there are no
observations.  Its taken me a while to get the data into the above format
and im afraid im stuck with how to get it into the final computational
format, so hopefully someone can help.

Perhaps i have to build a blank data frame with the appropriate dimensions
first but i am not sure if this is the most efficient way of accomplishing
this.  

Thanks in advance.

  
-- 
View this message in context: http://r.789695.n4.nabble.com/Data-frame-manipulation-tp2233932p2233932.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list