[R] Casting lists to data.frames, analog to SAS

David Winsemius dwinsemius at comcast.net
Wed Jan 14 22:14:05 CET 2009


On Jan 14, 2009, at 3:56 PM, Matthew Pettis wrote:

> <Resubmitting: Told I had a problem with a character set -- now
> submitting in plain text>
>
> I have a specific question and a general question.
>
> Specific Question: I want to do an analysis on a data frame by 2 or
> more class variables (i.e., use 2 or more columns in a dataframe to do
> statistical classing).  Coming from SAS, I'm used to being able to
> take a data set and have the output of the analysis in a dataset for
> further manipulation.  I have a data set with vote totals, with one
> column being the office name being voted on, and the other being the
> party of the candidate.  My votes are in the column "vc.n".  I did the
> analysis I want with:
>
> work <- by(sd62[,"vc.n"], sd62[,c("office.nm","party.abbr")], sum)
>
> the str() output of work looks like:
>
>> str(work)
> 'by' int [1:9, 1:11] NA 30 NA NA 0 0 0 NA 33 25678 ...
> - attr(*, "dimnames")=List of 2
>  ..$ office.nm : chr [1:9] "ATTORNEY GENERAL" "GOVERNOR & LT
> GOVERNOR" "SECRETARY OF STATE" "STATE AUDITOR" ...
>  ..$ party.abbr: chr [1:11] "CP" "DFL" "DFL2" "GP" ...
> - attr(*, "call")= language by.default(data = sd62[, "vc.n"], INDICES
> = sd62[, c("office.nm",      "party.abbr")], FUN = sum)

When I use as.data.frame.table on a similarly constructed object I get:

 > as.data.frame.table(with(warpbreaks, by(breaks, list(wool,  
tension), sum)))
   Var1 Var2 Freq
1    A    L  401
2    B    L  254
3    A    M  216
4    B    M  259
5    A    H  221
6    B    H  169

>
-- 
David Winsemius
>
>
>
>
> work is now a list.  I'd really like to have work be a data frame with
> 3 columns: The rows of the first two columns show the office and party
> levels being considered, and the third being the sum of the votes for
> that level combination.  How do I cast this list/output into a data
> frame?  using 'as.data.frame' doesn't work.
>
> General Question: I assume the answer to the specific question is
> dependent on my understanding list objects and accessing their
> attributes.  Can anyone point me to a good, throrough treatment of
> these R topics?  Specifically how to read and interpret the output of
> the str(), and attributes() function, how to extract the values of the
> 'by' output object into a data frame, etc.?
>
> Thanks,
> Matt
>
> ______________________________________________
> 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