Re: [R] By function

From: Liviu Andronic <landronimirc_at_gmail.com>
Date: Sat, 09 Apr 2011 17:33:34 +0200

Hello

On Fri, Apr 8, 2011 at 12:38 AM, Raoni Rodrigues <caciquesamurai_at_gmail.com> wrote:
> Hello all!
>
> I have a data frame with nine variables and 293 cases. (attached goes
> the csv file).
>
The CSV didn't get through so it's difficult to replicate your example. Please post the output of:
str(cpue)

> I need to calculate a index using the sum of one variable (N) divided
> by the length of other variable (Fisherman), but for each day (Date).
>
> I tried to use that codes:
>
> by (cpue, cpue$Date, function (x) sum (cpue$N)/length(cpue$Fisherman))
>
[..]

> In both codes, as result, I received the same value for all Date
> value. And, oddly, this result is the value of calculation for all
> data frame, as I used just:
>
> sum (cpue$N)/length(cpue$Fishermans)
>
>From the code above, this is expected since in the function()
statement you use the entire df (cpue) and not the subset (x). Try this:
by (cpue, cpue$Date, function (x) sum (x$N)/length(x$Fisherman))

> Sorry for the basic question, but I worked on this simple code all day
> and can't do it work out! And have no idea why...
>
> I'm using R 2.11 in Windows XP.
>
It is often a good a idea to post the output of sessionInfo()

Regards
Liviu

> Since now, thanks for the attention and all help,
>
> Best,
>
> Raoni
>
> ______________________________________________
> R-help_at_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.
>
>

-- 
Do you know how to read?
http://www.alienetworks.com/srtest.cfm
http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader
Do you know how to write?
http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail

______________________________________________
R-help_at_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.
Received on Sat 09 Apr 2011 - 15:38:54 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 10 Apr 2011 - 20:20:31 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive