![]() >CrossTable(gsstable,prop.t=FALSE,prop.r=FALSE,prop.c=FALSE,chisq=TRUE,format=c(“SPSS”)) Use the following lines of code to generate a cross table between ‘polviews’ and ‘partyid’ using the gsstable created above: The function is contained in the gmodels package and can be used on the table generated with the Table function above. Another quick method to generate tables is with the CrossTable function. There are options available with the Table function that include calculating row and column marginal totals as well a cell percentages. The following output results: polviews partyid EXTREMELY LIBERAL LIBERAL SLIGHTLY LIBERAL MODERATE STRONG DEMOCRAT 41 105 42 94 NOT STR DEMOCRAT 14 62 57 154 IND,NEAR DEM 11 47 57 103 INDEPENDENT 5 20 33 189 IND,NEAR REP 1 4 16 74 NOT STR REPUBLICAN 2 10 16 88 STRONG REPUBLICAN 0 5 5 22 OTHER PARTY 1 5 6 16 polviews partyid SLGHTLY CONSERVATIVE CONSERVATIVE EXTRMLY CONSERVATIVE STRONG DEMOCRAT 22 25 6 NOT STR DEMOCRAT 28 16 7 IND,NEAR DEM 25 11 5 INDEPENDENT 43 32 9 IND,NEAR REP 49 43 8 NOT STR REPUBLICAN 72 72 13 STRONG REPUBLICAN 23 101 27 OTHER PARTY 3 12 4 ![]() >gsstable gsstable #print table frequencies >#the variables partyid and polviews are used The following results in a frequency table of the variables “partyid” and “polviews” both of which are measured in categories: ![]() The Table function is a quick way to generate a cross tabulation table with a number of options available. R has a number of functions available for cross tabulations. Cross tabulations are often used to explore relationships among variables that are ordinal or categorical in nature. Survey data such as that found in the GSS file is usually a mixture of data types ranging from ratio level numbers to categorical data. Using the arrow keys, the home key, end key, and the page up and page down keys allows navigating and browsing the file. The following code will load the ‘.sav’ file: Make sure that the R packages Hmisc and foreign have been installed and loaded before attempting to import the SPSS file. Download the file to a directory that you can access from your R console.Īs discussed in a previous tutorial the SPSS format file can be loaded into an R data frame. From the next page choose SPSS to access ‘.sav’ format files and finally “2010” under the heading “GSS 1972-2012 Release 6.” Please note that this is a rather large data file with 2044 observations of 794 variables. The dataset is available from From that page click on the “Quick Downloads” link on the right hand side of the page to access the list of available datasets. There is a wealth of survey research data available at the NORC web site located at In this tutorial the dataset gss2010.sav will be used. Once the data has been imported into the R working environment it can be viewed and analyzed. In a previous tutorial I discussed how to import datasets from the NORC General Social Science Survey using R to write the SPSS formatted data to an R data frame. Using R to Work with GSS Survey Data: Viewing Datasets and Performing Cross Tabulations In the next tutorial I explore the use of the ca function to produce a basic Correspondence analysis of underlying dimensions in an N x N table. To view summary statistics for the table enter:Īs noted above xtabs is a quick and powerful function to create N x N tables with or without control variables. >#use xtabs to produce a table with a control variable To look at the relationship by gender use the following: The table created in the gsstab data frame shows the relationship between political ideology and political party affiliation. Variables used in social science research are often interrelated so it is desirable to control for one or more variables in order to further examine the variables of interest. This summary shows the number of cases in the table, the number of factors and the Chi-square value for the table. To view summary statistics generated use: >gssdataframe #use xtabs to produce a table >#get spss gss file and put into data frame >install.packages(“foreign”) #need for file import >install.packages(“Hmisc”) #need for file import Use the following code to load the data set into an R workspace: The focus of that tutorial was on the GSS2010 data set that was imported into the R workspace in SPSS format and then loaded into an R data frame for analysis. In Part Two of this series of tutorials I discussed how to find and import a data set from the NORC GSS survey. Using R to Work with GSS Survey Data Part Three: Using xtabs to Create and Analyze Tables
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |