Hi everyone. Could anyone help me to reshape my data? Reproducible example: firm<-sort(rep(1:1000,10),decreasing=F) year<-rep(1998:2007,1000) industry<-rep(c(rep(1,10),rep(2,10),rep(3,10),rep(4,10),rep(5,10),rep(6,10),rep(7,10),rep(8,10),rep(9,10), + rep(10,10)),1000) X1<-rnorm(10000) X2<-rnorm(10000,mean=0.5,sd=0.1) Y<-rnorm(10000,mean=0,sd=0.5) data<-data.frame(firm, industry,year,X1,X2,Y) With that data I have done: funcao<-function(data,...)(round(summary(lmList(Y~X1+X2| year, na.action=na.omit,data))$coefficients,3)) coef1<-daply(data,.(industry),"funcao") coef2<-aperm(coef1,c(2,4,3,1),resize=TRUE) coef3<-coef2[,,"Estimate",] Now I have an array with the coefficients of lmList but I need a dataframe with this columns: Year Industry (Estimate) X1 X2 I have tried melt () from package reshape, but it doesn?t do what I want (or I don?t know how to transform the output it in a dataframe). Could anyone help me? Thanks in advance, Cec?lia Carmo (Universidade de Aveiro - Portugal)
Patrick Connolly
2009-Aug-11 20:00 UTC
[R] Transform array in dataframe; melt(); reshape()
On Tue, 11-Aug-2009 at 07:15PM +0100, Cecilia Carmo wrote:> Hi everyone. > > Could anyone help me to reshape my data? > > Reproducible example: > firm<-sort(rep(1:1000,10),decreasing=F) > year<-rep(1998:2007,1000) > industry<-rep(c(rep(1,10),rep(2,10),rep(3,10),rep(4,10),rep(5,10),rep(6,10),rep(7,10),rep(8,10),rep(9,10), > + rep(10,10)),1000) > X1<-rnorm(10000) > X2<-rnorm(10000,mean=0.5,sd=0.1) > Y<-rnorm(10000,mean=0,sd=0.5) > data<-data.frame(firm, industry,year,X1,X2,Y) > > With that data I have done: > funcao<-function(data,...)(round(summary(lmList(Y~X1+X2| year, > na.action=na.omit,data))$coefficients,3)) > coef1<-daply(data,.(industry),"funcao") > coef2<-aperm(coef1,c(2,4,3,1),resize=TRUE) > coef3<-coef2[,,"Estimate",] > > Now I have an array with the coefficients of lmList but I need a > dataframe with this columns: > Year Industry (Estimate) X1 X2 >> > I have tried melt () from package reshape, but it doesn?t do what I want > (or I don?t know how to transform the output it in a dataframe). Could > anyone help me?You need to use the function cast as well. -- ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~. ___ Patrick Connolly {~._.~} Great minds discuss ideas _( Y )_ Average minds discuss events (:_~*~_:) Small minds discuss people (_)-(_) ..... Eleanor Roosevelt ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.