[R] How to deal with a dataframe within a dataframe?

R. Michael Weylandt michael.weylandt at gmail.com
Tue May 8 15:38:51 CEST 2012


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
>
>
>> agg <- aggregate(formula=df$value ~ df$quarter + df$tool,
> +     FUN=cp.cpk, lsl=1300, usl=1500)
>> head(agg)
>  df$quarter df$tool           df$value
> 1       09Q3    VS1A 1.800534, 1.628483
> 2       10Q1    VS1A 1.299652, 1.261302
> 3       10Q2    VS1A 1.699018, 1.381570
> 4       10Q3    VS1A 1.311681, 1.067232
>> head(agg["df$value"])
>            df$value
> 1 1.800534, 1.628483
> 2 1.299652, 1.261302
> 3 1.699018, 1.381570
> 4 1.311681, 1.067232
>> class(agg["df$value"])
> [1] "data.frame"
>> head(agg["df$value"][1]) # trying to select 1st column
>            df$value
> 1 1.800534, 1.628483
> 2 1.299652, 1.261302
> 3 1.699018, 1.381570
> 4 1.311681, 1.067232
>> head(agg["df$value"][2]) # trying to select 2nd column
> Error in `[.data.frame`(agg["df$value"], 2) : undefined columns selected
>>
>
>
> # FWIW, here's the aggregating function
> function(data, lsl, usl) {
>    if (length(data) < 15) {
>        return(NA)
>    } else {
>        return (c(
>            (usl-lsl)/(6*sd(data)),
>            min(mean(data)-lsl, usl-mean(data))/(3*sd(data)))
>        )
>    }
> }
>
> ______________________________________________
> 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.



More information about the R-help mailing list