Hello all,
I am doing an aggregation where the aggregating function returns not a
single numeric value but a vector of two elements using return(c(val1,
val2)). I don't know how to access the individual columns of that
vector in the resulting dataframe though. How is this done correctly?
Thanks, robert
So this actually looks like something of a tricky one: if you wouldn't
mind sending the result of dput(head(agg)) I can confirm, but here's
my hunch:
Try this:
agg2 <- aggregate(len ~ ., data = ToothGrowth, function(x) c(min(x), max(x)))
print(agg2)
str(agg2)
You'll see that the third "column" is actually a matrix that has two
columns: so what you really need is
agg2[,3][,1]
if you want the mins.
What's funny is that this doesn't work for you (as checking the class
suggests by giving df and then confirmed with what i would have
guessed worked on the column side. )
Instead, it looks like your data somehow got stuck together (possibly
as factors?) -- either way, I think you need to use double brackets to
get the inner multi-column structure to take a look at it:
agg[["df$value"]][,1]
or more easy, specify column subsetting (which will use df-ness and
not list-ness)
agg[, "df$value"][,1]
Anyways, hope this gets you on the right track and with
dput(head(agg)) we can definitely figure this out.
Best,
Michael
On Tue, May 8, 2012 at 9:19 AM, Robert Latest <boblatest at gmail.com> wrote:
Hello all,
I am doing an aggregation where the aggregating function returns not a
single numeric value but a vector of two elements using return(c(val1,
val2)). I don't know how to access the individual columns of that
vector in the resulting dataframe though. How is this done correctly?
Thanks, robert
I would like this in either the form of a "flat" data frame (i.e., the
contents of "df$value" as two separate columns), or -- even preferable
-- learn a better way to retrieve multiple numeric results from a call
to aggregate().
Thanks,
robert
I would like this in either the form of a "flat" data frame (i.e., the
contents of "df$value" as two separate columns), or -- even preferable
-- learn a better way to retrieve multiple numeric results from a call
to aggregate().
The reason you are having difficulty is a) that you have somehow
(noting that you have omitted all context) managed to construct
column names with dollar-signs in them which the interpreter attempts
to parse as a function and then b) the 'df$value' column is also a
list rather than an atomic vector. It's a rather pathological
construct in my opinion, but maybe one of the masteRs with think
differently. This will pull the first element of that column's third
entry:
> agg[3,3][[1]][1]
[1] 1.699018
This will return all of the first entries:
sapply(1:6, function(x) agg[x, 3][[1]][1])
[1] 1.800534 1.299652 1.699018 1.311681 1.541658 1.299252
You might start by renaming that objects columns with valid R names.
David.
>
> Thanks,
> robert
>
> ______________________________________________
> 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.
David Winsemius, MD
West Hartford, CT
On Tue, May 8, 2012 at 9:19 AM, Robert Latest <boblatest at gmail.com> wrote:
Hello all,
I am doing an aggregation where the aggregating function returns not a
single numeric value but a vector of two elements using return(c(val1,
val2)). I don't know how to access the individual columns of that
vector in the resulting dataframe though. How is this done correctly?
Thanks, robert