[R] Cross tabulation with top one variable and side as multiple variables

David L Carlson dcarlson at tamu.edu
Thu Jun 18 17:09:58 CEST 2015


They do not match because xtabs() in R produces a multidimensional array (one dimension for each variable). Looking at your spreadsheet on nabble, it appears that SPSS is just creating 4 crosstabulations with TREND against each of the other variables. That is easily done in R, but for tested code, you need to give us reproducible data using dput(). I get an error using read.spss() on your uploaded file. You should also read some of the extensive free documentation available on R. The ftable() function creates a two dimensional representation of that 5-dimensional array. But your spreadsheet is just a stack of two-dimensional tables. You could get there with the margin.table() function, but unless you really need the 5-dimensional array, you probably want something more like:

rowvars <- c("AGET", "SEXT", "EDUCRT",  "JOBRT")
table.lst <- lapply(rowvars, function(x) xtabs(~x+TREND))

That would give you a list containing a crosstabulation table between each of the variables and TREND. A spreadsheet with 2000 tables seems a bit unwieldy so you might want to give some thought to what you really want as output.

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of jagadishpchary
Sent: Thursday, June 18, 2015 1:46 AM
To: r-help at r-project.org
Subject: Re: [R] Cross tabulation with top one variable and side as multiple variables

I think my explanation in the post is not giving the full details on the job
to be done. Sorry for that. Here is what I am doing..

1.	I have a SPSS data set with more than 2000 variables. However for test
purpose I have created a temporary data set with 5 variables which I am
reading it to R environment (Attached the test.sav file).
2.	There is a variable called “TREND” which has the year data. So all I need
to do is cross tabulate the variables with this Trend variable. 
In SPSS the syntax would be

CTABLES
/VLABELS VARIABLES =ALL DISPLAY=LABEL
/TABLES (AGET +SEXT +EDUCRT +JOBRT ) [COUNT F40.0] by TREND.

The final cross tabulation results are placed in the attached excel report
with sheet name “Results”.

As I am new to R  - I tried searching the forums for the cross tabulation
with top variable constant and multiple variables as side however I could
not find it. Anyhow I tried using the below syntax :

Xtabs ( ~ AGET +SEXT +EDUCRT +JOBRT + TREND, data=mydata)
summary(~AGET +SEXT +EDUCRT +JOBRT, data= mydata, fun=table)
ftable (mydata, row.vars=c("AGET ", " SEXT ", " EDUCRT " , “JOBRT”),
col.vars="TREND")

the results are not identical to what I am getting in SPSS

Hence I would request to suggest me a R code that helps me in getting the
results as shown in the attached excel report with sheet name “Results”.
Test.sav <http://r.789695.n4.nabble.com/file/n4708799/Test.sav>  
Cross_tabulation.xlsx
<http://r.789695.n4.nabble.com/file/n4708799/Cross_tabulation.xlsx>  




--
View this message in context: http://r.789695.n4.nabble.com/Cross-tabulation-with-top-one-variable-and-side-as-multiple-variables-tp4708379p4708799.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
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