[R] Help with tables in R

Jim Lemon jim at bitwrit.com.au
Thu Jul 3 12:16:54 CEST 2014


On Wed, 2 Jul 2014 09:33:12 PM Barry Lambert wrote:
> I am a new convert to R (from SAS).  I am a research scientist and 
most of
> my
> use of SAS was in data analysis.  Recently, I have been wanting to 
use R to
> create a simple, reliable way to summarize a dataset of student 
demographics
> for a university.
> The spreadsheet has a row for every student registered at the 
university for
> each term since fall 2010 with the following information about each 
student
> in columns:
> Columns are the following: Term, College, Program, Campus, 
Gender,
> Ethnicity, Age.
> 
> I have created summary tables in Excel using if/then type formulas 
to select
> data and count the number of female students in program A at 
location 3,
> etc.
> 
> I have written some R code to create some figures that generally 
meet my
> needs.
> I would like to find a way to have R populate some tables with this 
type of
> information.
> 
> An example of my excel sheets are attached.
> 
> 
> Any suggestions will be appreciated.

Hi Barry,
If I understand the above properly, you are calculating counts of 
female students in different programs and locations. I would do this 
using the "by" function with "length" as the function called for each 
group defined by program and location. This will produce an array of 
counts, and your problem is then to add a table of values to the plot. 
Here is an example:

bldat<-data.frame(sex=sample(c("M","F"),1000,TRUE),
 year=rep(2010:2013,each=250),
 program=sample(c("Arts","Math","Science"),1000,TRUE),
 location=sample(c("New York","London","Paris"),1000,TRUE))
females<-bldat$sex=="F"
femsumm<-by(bldat$sex,bldat[,c("program","location")],
 FUN=length)
rownames(femsumm)<-c("Arts","Math","Science") 
barplot(femsumm,beside=TRUE,ylim=c(0,150))
library(plotrix)
addtable2plot(4,120,femsumm,display.rownames=TRUE)

Jim



More information about the R-help mailing list