[R] Newbie - Summarize function

Gabor Grothendieck ggrothendieck at gmail.com
Wed Dec 21 18:16:41 CET 2005


Look at ?summarize .  The FUN argument is supposed to be a function
that inputs a vector but your g inputs a data frame.  I think you want
something like this (assuming package Hmisc):

   mApply(test, test[,1:2], g)

On 12/20/05, Andrew.Haywood at poyry.com.au <Andrew.Haywood at poyry.com.au> wrote:
> Dear R Users,
>
> I have searched through the archives but I am still struggling to find a
> way to process the below dataset. I have a dataset that has stratum and
> plot identifier. Within each plot there is variable (Top) stating the
> number of measurments that should be used to to calculate the mean to the
> largest "top" elements within one of the vectors (X). I would like to
> process this summary statistic by groups. At this stage I have been trying
> to use the "summarize" function within the Hmisc library but I am getting
> the following error "Error in eval(expr, envir, enclos) : numeric 'envir'
> arg not of length one In addition: Warning message: no finite arguments to
> max; returning -Inf".
>
> Any suggetsions on how I can fix this would be greatly appreciated.
>
> Kind regards
>
> Andrew
>
> test <- read.table("test.csv", header=TRUE, sep=",")
> #function to calculate mean of "top" elements within a plot
> > g<-function(y) {
> + top_no <-max(y$top)
> + weight <- with(y,as.numeric(x>=x[order(x,decreasing=TRUE)[top_no]]))
> + wtd.mean(y$x,weight)
> + }
> > g(test)
> [1] 172.6667
> #call to summarize function - use function g and summarise by stratum plot
> > test.2 <- with(test,summarize(test$x,llist(test$Stratum,test$plot),g))
> Error in eval(expr, envir, enclos) : numeric 'envir' arg not of length one
> In addition: Warning message:
> no finite arguments to max; returning -Inf
>
> >traceback()
> 9: eval(substitute(expr), data, enclos = parent.frame())
> 8: with.default(y, as.numeric(x >= x[order(x, decreasing =
> TRUE)[top_no]]))
> 7: with(y, as.numeric(x >= x[order(x, decreasing = TRUE)[top_no]]))
> 6: FUN(X, ...)
> 5: summarize(test$x, llist(test$Stratum, test$plot), g)
> 4: eval(expr, envir, enclos)
> 3: eval(substitute(expr), data, enclos = parent.frame())
> 2: with.default(test, summarize(test$x, llist(test$Stratum, test$plot),
>       g))
> 1: with(test, summarize(test$x, llist(test$Stratum, test$plot),
>       g))
>
> The version im running on is
>
> $platform
> [1] "i386-pc-mingw32"
>
> $arch
> [1] "i386"
>
> $os
> [1] "mingw32"
>
> $system
> [1] "i386, mingw32"
>
> $status
> [1] ""
>
> $major
> [1] "2"
>
> $minor
> [1] "2.0"
>
> $year
> [1] "2005"
>
> $month
> [1] "10"
>
> $day
> [1] "06"
>
> $"svn rev"
> [1] "35749"
>
> $language
> [1] "R"
>
> >
>
>
> Stratum plot     id        top     x
> 1       1       1       2       12
> 1       1       2       2       41
> 1       1       3       2       12
> 1       1       4       2       43
> 1       1       5       2       12
> 1       1       6       2       14
> 1       1       7       2       43
> 1       1       8       2       12
> 1       2       1       4       42
> 1       2       2       4       12
> 1       2       3       4       432
> 1       2       4       4       12
> 1       2       5       4       12
> 1       2       6       4       14
> 1       2       7       4       41
> 1       2       8       4       1
> 2       1       1       2       12
> 2       1       2       2       41
> 2       1       3       2       12
> 2       1       4       2       43
> 2       1       5       2       12
> 2       1       6       2       14
> 2       1       7       2       43
> 2       1       8       2       12
> 2       2       1       3       42
> 2       2       2       3       12
> 2       2       3       3       432
> 2       2       4       3       12
> 2       2       5       3       12
> 2       2       6       3       14
> 2       2       7       3       41
> 2       2       8       3       1
>
>
>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list