function to include factors in summary data frame

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

function to include factors in summary data frame

Wade Wall
Hi all,

I have a dataframe that includes data on individuals that are distributed
across multiple rows.  I have aggregated the data using ddply, but I have
columns in the original data frame that are factors ( such as sites "A",
"B", and "C") that I would like to include in the new data frame.  I have
done this in a clunky way using match() and a loop, but am wondering if
there is a more elegant approach.  Here is an example data set.

#Example
a<-c(rep(1:5,6)); b<-sort(b)
b<-c(rep("A",10),rep("B",10),rep("C",10))
a<-c(rep(1:5,6)); b<-sort(b)
d<-c(2008,2008,2009,2009,2010,2010);d<-rep(d,5)
e<-rnorm(30,2,1)
df<-data.frame(a,b,d,e) ; names(df)<-c("ind","site","year","height")

I created a new factor using ind and year, and would basically like to
include site in the new dataframe. Does anyone know how this could easily be
done?

Thanks in advance.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: function to include factors in summary data frame

Daniel Malter
I have read it three times and still no concrete idea what you are actually trying to do, mainly because there is no information as to which level/variable you are aggregating on. It'd help if you provided the aggregated data (or sample rows thereof) so that we know what you want the result to be.

Best,
Daniel

Wade Wall wrote
Hi all,

I have a dataframe that includes data on individuals that are distributed
across multiple rows.  I have aggregated the data using ddply, but I have
columns in the original data frame that are factors ( such as sites "A",
"B", and "C") that I would like to include in the new data frame.  I have
done this in a clunky way using match() and a loop, but am wondering if
there is a more elegant approach.  Here is an example data set.

#Example
a<-c(rep(1:5,6)); b<-sort(b)
b<-c(rep("A",10),rep("B",10),rep("C",10))
a<-c(rep(1:5,6)); b<-sort(b)
d<-c(2008,2008,2009,2009,2010,2010);d<-rep(d,5)
e<-rnorm(30,2,1)
df<-data.frame(a,b,d,e) ; names(df)<-c("ind","site","year","height")

I created a new factor using ind and year, and would basically like to
include site in the new dataframe. Does anyone know how this could easily be
done?

Thanks in advance.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: function to include factors in summary data frame

PIKAL Petr
In reply to this post by Wade Wall
>
> Hi all,
>
> I have a dataframe that includes data on individuals that are
distributed
> across multiple rows.  I have aggregated the data using ddply, but I
have
> columns in the original data frame that are factors ( such as sites "A",
> "B", and "C") that I would like to include in the new data frame.  I
have

> done this in a clunky way using match() and a loop, but am wondering if
> there is a more elegant approach.  Here is an example data set.
>
> #Example
> a<-c(rep(1:5,6)); b<-sort(b)
> b<-c(rep("A",10),rep("B",10),rep("C",10))
> a<-c(rep(1:5,6)); b<-sort(b)
> d<-c(2008,2008,2009,2009,2010,2010);d<-rep(d,5)
> e<-rnorm(30,2,1)
> df<-data.frame(a,b,d,e) ; names(df)<-c("ind","site","year","height")

Do you want something like this?

with(df, aggregate(height, list(ind=ind, year=year, site=site), mean))

or
ddply(df, .(ind,year,site), summarise, mean(height))

Regards
Petr



>
> I created a new factor using ind and year, and would basically like to
> include site in the new dataframe. Does anyone know how this could
easily be

> done?
>
> Thanks in advance.
>
>    [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: function to include factors in summary data frame

Wade Wall
That is exactly what I was looking for.  Thanks, Wade

On Wed, Sep 14, 2011 at 5:47 AM, Petr PIKAL <[hidden email]> wrote:

> >
> > Hi all,
> >
> > I have a dataframe that includes data on individuals that are
> distributed
> > across multiple rows.  I have aggregated the data using ddply, but I
> have
> > columns in the original data frame that are factors ( such as sites "A",
> > "B", and "C") that I would like to include in the new data frame.  I
> have
> > done this in a clunky way using match() and a loop, but am wondering if
> > there is a more elegant approach.  Here is an example data set.
> >
> > #Example
> > a<-c(rep(1:5,6)); b<-sort(b)
> > b<-c(rep("A",10),rep("B",10),rep("C",10))
> > a<-c(rep(1:5,6)); b<-sort(b)
> > d<-c(2008,2008,2009,2009,2010,2010);d<-rep(d,5)
> > e<-rnorm(30,2,1)
> > df<-data.frame(a,b,d,e) ; names(df)<-c("ind","site","year","height")
>
> Do you want something like this?
>
> with(df, aggregate(height, list(ind=ind, year=year, site=site), mean))
>
> or
> ddply(df, .(ind,year,site), summarise, mean(height))
>
> Regards
> Petr
>
>
>
> >
> > I created a new factor using ind and year, and would basically like to
> > include site in the new dataframe. Does anyone know how this could
> easily be
> > done?
> >
> > Thanks in advance.
> >
> >    [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] 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]]

______________________________________________
[hidden email] 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.