[R] By function

Raoni Rodrigues caciquesamurai at gmail.com
Sun Apr 10 22:00:19 CEST 2011


Liviu,

Thanks for your attention and help!

Sorry for attached csv file, I didn't read R-Help Posting guide
careful. Another R-helper thaught me use de dput() function, and this
output are in email's end.

Your correction in my code works pretty well!! Now I can understand a
little more how the R "think"! Thanks a lot!

Also thanks for the suggetion to use sessionInfo(). I will do that on
next questions to list.

All best, and thanks very much again,

Raoni

The dput() of data is:

structure(list(Data = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L,
4L, 4L, 4L, 4L, 5L, 5L, 5L), .Label = c("2011-02-04", "2011-02-05",
"2011-02-07", "2011-02-08", "2011-02-09", "2011-02-10", "2011-02-11",
"2011-02-14", "2011-02-15", "2011-02-16", "2011-02-17", "2011-02-18",
"2011-02-21", "2011-02-22", "2011-02-23", "2011-02-24", "2011-02-25",
"2011-02-28", "2011-03-01", "2011-03-02", "2011-03-03", "2011-03-04",
"2011-03-10", "2011-03-11", "2011-03-14", "2011-03-15", "2011-03-16",
"2011-03-17", "2011-03-28", "2011-03-29", "2011-03-30", "2011-03-31",
"2011-04-01", "2011-04-04", "2011-04-05", "2011-04-06", "2011-04-07"
), class = "factor"), Fisherman = structure(c(5L, 1L, 2L,
5L, 1L, 2L, 1L, 2L, 5L, 5L, 2L, 1L, 2L, 5L, 1L, 2L, 5L, 1L, 5L,
5L, 2L, 1L, 5L, 1L, 2L, 5L, 1L, 2L, 5L, 1L), .Label = c("Yellow",
"Blue", "White", "Green", "Red"), class = "factor"), N = c(0L,
1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("Date",
"Fisherman", "N"), row.names = c(NA, 30L), class = "data.frame")


2011/4/9 Liviu Andronic <landronimirc at gmail.com>:
> 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
>



More information about the R-help mailing list