about data manipulation

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

about data manipulation

lily li
Hi R users,

I'm trying to manipulate dataset, but met some difficulties.

df
year   month   flow
2006     3        3.5
2006     4        3.8
2006     5        21
2006     6        32
2007     3        4.1
2007     4        4.4
...

I want to calculate total flow for each year, and use the code below:
aggregate(flow~year, data=df, sum)
But it gave the error message:
Error in get(as.character(FUN), mode = "function", envir = envir) :
  object 'FUN' of mode 'function' was not found

What is the problem and how to solve it? Thanks for your help.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: about data manipulation

Berend Hasselman

> On 1 Dec 2016, at 05:06, lily li <[hidden email]> wrote:
>
> Hi R users,
>
> I'm trying to manipulate dataset, but met some difficulties.
>
> df
> year   month   flow
> 2006     3        3.5
> 2006     4        3.8
> 2006     5        21
> 2006     6        32
> 2007     3        4.1
> 2007     4        4.4
> ...
>
> I want to calculate total flow for each year, and use the code below:
> aggregate(flow~year, data=df, sum)
> But it gave the error message:
> Error in get(as.character(FUN), mode = "function", envir = envir) :
>  object 'FUN' of mode 'function' was not found
>
> What is the problem and how to solve it? Thanks for your help.
>

Not enough information.
If I try this

df <- read.table(text="year   month   flow
2006     3        3.5
2006     4        3.8
2006     5        21
2006     6        32
2007     3        4.1
2007     4        4.4
", header=TRUE)

df

aggregate(flow~year, data=df, sum)

I get a correct answer. So you are likely doing something weird and not showing us all.


> [[alternative HTML version deleted]]
>

Plan text mail. Has been asked many, many times before.


Berend Hasselman


> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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: about data manipulation

Jim Lemon-4
In reply to this post by lily li
Hi lily,
If you want to use aggregate, supply the name of the function:

aggregate(flow~year, data=df, "sum")

You can also use "by" like this

by(df$flow,df$year,FUN=sum)

I assume that you don't have to worry about missing months in a year.

Jim
:


On Thu, Dec 1, 2016 at 3:06 PM, lily li <[hidden email]> wrote:

> Hi R users,
>
> I'm trying to manipulate dataset, but met some difficulties.
>
> df
> year   month   flow
> 2006     3        3.5
> 2006     4        3.8
> 2006     5        21
> 2006     6        32
> 2007     3        4.1
> 2007     4        4.4
> ...
>
> I want to calculate total flow for each year, and use the code below:
> aggregate(flow~year, data=df, sum)
> But it gave the error message:
> Error in get(as.character(FUN), mode = "function", envir = envir) :
>   object 'FUN' of mode 'function' was not found
>
> What is the problem and how to solve it? Thanks for your help.
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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.