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

Matthew Pettis matthew.pettis at gmail.com
Wed Jan 14 21:56:37 CET 2009


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




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




More information about the R-help mailing list