[R] How to deal with a dataframe within a dataframe?
David Winsemius
dwinsemius at comcast.net
Wed May 9 15:14:33 CEST 2012
On May 9, 2012, at 2:40 AM, Robert Latest wrote:
> On Tue, May 8, 2012 at 3:38 PM, R. Michael Weylandt
> <michael.weylandt at gmail.com> wrote:
>> 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:
>
> Hi Michael,
>
> while I'm trying to get my head around the rest of your post, here's
> the output of dput():
>
>> dput(head(agg))
> structure(list(`df$quarter` = c("09Q3", "10Q1", "10Q2", "10Q3",
> "11Q1", "11Q2"), `df$tool` = structure(c(1L, 1L, 1L, 1L, 1L,
> 1L), .Label = c("VS1A", "VS1B", "VS2A", "VS2B", "VS3A", "VS3B",
> "VS4A", "VS4B", "VS5B"), class = "factor"), `df$value` =
> structure(list(
> `0` = c(1.80053430839867, 1.62848325226279), `1` =
> c(1.29965212329278,
> 1.26130173276939), `2` = c(1.69901753654472, 1.38156952313768
> ), `3` = c(1.31168126092175, 1.06723157138633), `4` =
> c(1.54165763354293,
> 1.21619657757276), `5` = c(1.29925171313276, 1.18276707678292
> )), .Names = c("0", "1", "2", "3", "4", "5"))), .Names = c("df
> $quarter",
> "df$tool", "df$value"), row.names = c(NA, 6L), class = "data.frame")
>>
>
> 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
More information about the R-help
mailing list