Hi everyone!
I have this dataframe:
firm<-c(rep(1,4),rep(2,4),rep(3,4),rep(4,4),rep(5,4),rep(6,4))
year<-c(rep(2000:2003,6))
industry<-c(rep(10,4),rep(20,4),rep(30,4),rep(10,4),rep(20,4),rep(30,4))
X1<-c(10,14,18,16,20,45,23,54,24,67,98,58,16,32,57,12,54,0,0,22,11,3,5,6)
data<-data.frame(firm, industry,year,X1)
data
I need a loop that calculates the mean of X1 by year and
by industry and I need this values in a dataframe or a
matrix.
I have done:
means<-matrix(nrow=3,ncol=4)
for ( i in unique(data$industry))
for (j in
data$year){means[i,j]<-mean(data$X1,na.rm=TRUE)}
But it doesn?t work. Could anyone help me?
Cec?lia Carmo
Dear Cecilia, Here is one way: with(<yourdata>, tapply(X1, list(year, industry), mean)) Also, take a look at ?ave and its examples. HTH, Jorge On Sun, Jun 28, 2009 at 12:39 PM, Cecilia Carmo <cecilia.carmo@ua.pt> wrote:> Hi everyone! > > I have this dataframe: > > firm<-c(rep(1,4),rep(2,4),rep(3,4),rep(4,4),rep(5,4),rep(6,4)) > year<-c(rep(2000:2003,6)) > industry<-c(rep(10,4),rep(20,4),rep(30,4),rep(10,4),rep(20,4),rep(30,4)) > X1<-c(10,14,18,16,20,45,23,54,24,67,98,58,16,32,57,12,54,0,0,22,11,3,5,6) > data<-data.frame(firm, industry,year,X1) > data > > I need a loop that calculates the mean of X1 by year and by industry and I > need this values in a dataframe or a matrix. > I have done: > > means<-matrix(nrow=3,ncol=4) > > for ( i in unique(data$industry)) > for (j in data$year){means[i,j]<-mean(data$X1,na.rm=TRUE)} > > But it doesn´t work. Could anyone help me? > > Cecília Carmo > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Hi Cecilia,
Trying it your way there where three reasons for errors, I fixed them in the
following code:
means<-matrix(nrow=3,ncol=4)
counter.i <- 0
counter.j <- 0
for (i in levels(factor(data$industry)))
{
counter.i <- counter.i + 1
for (j in levels(factor(data$year)))
{
counter.j <- counter.j + 1
means[counter.i,counter.j]<- mean(data$X1 [data$industry == i &
data$year
== j] ,na.rm=TRUE)
}
counter.j <- 0
}
means
Also consider ddply in the plyr package (although that's an over kill if
your only having two loops)
Or Jorge solution.
Cheers,
Tal G
On Sun, Jun 28, 2009 at 7:39 PM, Cecilia Carmo <cecilia.carmo@ua.pt>
wrote:
> for ( i in unique(data$industry))
> for (j in data$year){means[i,j]<-mean(data$X1,na.rm=TRUE)}
>
--
----------------------------------------------
My contact information:
Tal Galili
Phone number: 972-50-3373767
FaceBook: Tal Galili
My Blogs:
http://www.r-statistics.com/
http://www.talgalili.com
http://www.biostatistics.co.il
[[alternative HTML version deleted]]