Re: [R] Applying by() when groups have different lengths [RESOLVED]

2018-09-17 Thread Bert Gunter
" I did not pick up on by() doing the splitting for me when I read the help..." >From ?by: "A data frame is split by row into data frames subsetted by the values of one or more factors, and function FUN is applied to each subset in turn." I do not understand how it could be more clearly stated t

Re: [R] Applying by() when groups have different lengths

2018-09-17 Thread Rich Shepard
On Mon, 17 Sep 2018, Bert Gunter wrote: I cannot think of a way of proving a sample of data; if a sample for a Typo: s/proving/providing/ Rich __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/li

Re: [R] Applying by() when groups have different lengths

2018-09-17 Thread Bert Gunter
Inline. Bert On Mon, Sep 17, 2018 at 11:54 AM Rich Shepard wrote: >My dataframe has 113K rows split by a factor into 58 separate > data.frames, > with a different numbers of rows (see error output below). > >I cannot think of a way of proving a sample of data; if a sample for a > MWE

Re: [R] Applying by() when groups have different lengths [RESOLVED]

2018-09-17 Thread Rich Shepard
On Mon, 17 Sep 2018, MacQueen, Don wrote: I'm also going to guess that maybe your object rainfall_by_site has already been split into separate data frames (because of its name). But by() does the splitting internally, so you should be passing it the original unsplit data frame. Don, I did n

Re: [R] Applying by() when groups have different lengths

2018-09-17 Thread MacQueen, Don via R-help
I'm also going to guess that maybe your object rainfall_by_site has already been split into separate data frames (because of its name). But by() does the splitting internally, so you should be passing it the original unsplit data frame. You could supply example data by providing the first few

Re: [R] Applying by() when groups have different lengths

2018-09-17 Thread MacQueen, Don via R-help
Try changing it to by(rainfall_by_site, rainfall_by_site[, 'name'], function(x) {mean.rain <- mean(x[, 'prcp']) }) Inside the function, so to speak, the function sees an object named "x", because that's how the function is defined: function(x). So you have to operate on x inside

Re: [R] Applying by() when groups have different lengths

2018-09-17 Thread William Dunlap via R-help
> > by(rainfall_by_site, rainfall_by_site[, 'name'], function(x) { > + mean.rain <- mean(rainfall_by_site[, 'prcp']) + }) Note that you define a function of x which does not use x in it. Hence, even if the function gave a value, it would give the same value for each group. To see what the 'x' in

[R] Applying by() when groups have different lengths

2018-09-17 Thread Rich Shepard
My dataframe has 113K rows split by a factor into 58 separate data.frames, with a different numbers of rows (see error output below). I cannot think of a way of proving a sample of data; if a sample for a MWE is desired advice on produing one using dput() is needed. To summarize each group