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

Robert Latest boblatest at gmail.com
Tue May 8 15:19:12 CEST 2012


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)))
        )
    }
}



More information about the R-help mailing list