Transforming Variables in R

Chapter 4 Transforming Variables

4.1 Get Ready

  • R Libraries: Load the following R libraries needed for this chapter:

    • descr

    • Hmisc

  • Data Files: Load the following data files:

    • states20.rda

    • anes20.rda

  • You may need to install packages if not previously done.

  • Useful Function: Use cut2 from the Hmisc package to collapse numeric variables into binned categories.

  • Working Directory: Ensure that datasets are in your working directory to avoid specifying file paths.

4.2 Introduction

  • Before exploring advanced graphing and statistical techniques, it is essential to transform the data into a more usable form in R.

  • Simplifying Names: Use intuitive variable names to make the dataset easy to work with.

  • Transformations: Will cover:

    • Renaming variables

    • Reordering categories

    • Combining variables

  • These transformations are vital for effective data analysis and will aid in future course assignments.

4.3 Data Transformations

  • Modifications to datasets are often necessary for effective analysis.

  • Modifications include adjusting single variables, multiple variables, or entire datasets.

  • Importance of Record Keeping:

    • Maintain script files with all commands used for transformations.

    • Script files serve as documentation for transformations for current and future projects.

4.4 Renaming and Relabeling

  • Changing Variable Names: Renaming cumbersome variables can simplify data analysis.

    • Example:

    • Original Variable Names: V201314x (welfare programs), V201320x (aid to the poor)

    • New Variable Names: anes20welfarespnd</code>and<code>anes20welfare_spnd</code> and <code>anes20poor_spnd.

    • Copying variables to new objects with meaningful names enhances comprehension.

    • For instance:

    • anes20welfarespnd<−anes20welfare_spnd <- anes20V201314x

    • anes20poorspnd<−anes20poor_spnd <- anes20V201320x

    • Ensure new variables remain within the anes20 dataset for analysis integrity.

    • Verification: Always check variable frequencies to confirm correct transformations.

4.4.1 Changing Attributes

  • Variable Class: R classifies non-numeric variables as factors.

  • Ordered vs Unordered Factors: Some variables, like spending preference, are ordered:

    • Confirm with commands: class(anes20poorspnd)</code>and<code>levels(anes20poor_spnd)</code> and <code>levels(anes20poor_spnd).

  • Changing a factor variable to ordered:

    • Use: anes20poorspnd<−ordered(anes20poor_spnd <- ordered(anes20poor_spnd).

    • Verify by checking the class and levels again.

Value Labels

  • Modify category labels when original labels are too long or unwieldy (as seen in graphs).

  • Use the levels() function to change labels permanently:

    • Original categories and new labels are defined for clarity in reporting and visualization.

    • Example: levels(anes20poorspnd)<−c("Increase/Lot","Increase","Same","Decrease","Decrease/Lot")</code>.</p></li></ul></li></ul><h4id="1d974ad0−620f−4ef8−8aa4−0dcea5b1ea3b"data−toc−id="1d974ad0−620f−4ef8−8aa4−0dcea5b1ea3b"collapsed="false"seolevelmigrated="true">CollapsingandReorderingCategories</h4><ul><li><p>Sometimesitisnecessarytoalterthenumberofcategorieswithinavariable.</p></li><li><p>Examplewith<code>anes20poor_spnd) <- c("Increase/Lot", "Increase", "Same", "Decrease", "Decrease/Lot")</code>.</p></li></ul></li></ul><h4 id="1d974ad0-620f-4ef8-8aa4-0dcea5b1ea3b" data-toc-id="1d974ad0-620f-4ef8-8aa4-0dcea5b1ea3b" collapsed="false" seolevelmigrated="true">Collapsing and Reordering Categories</h4><ul><li><p>Sometimes it is necessary to alter the number of categories within a variable.</p></li><li><p>Example with <code>anes20poor_spnd: Collapse five categories into three:

      • Create a new variable:

      • anes20poorspnd.3<−(anes20poor_spnd.3 <- (anes20poor_spnd)

      • Define new levels: levels(anes20poorspnd.3)<−c("Increase","Increase","KeepSame","Decrease","Decrease")</code>.</p></li></ul></li><li><p><strong>CheckTransformations</strong>:Usebarplottovisualizecategorydistributionspost−transformation.</p></li></ul><h4id="bc981b7b−b1fa−4588−a8e1−0607694f0a6c"data−toc−id="bc981b7b−b1fa−4588−a8e1−0607694f0a6c"collapsed="false"seolevelmigrated="true">ReorderingCategories</h4><ul><li><p>Toensurelogicalorderingofcategories(e.g.,fromlowtohigh),usethe<code>ordered</code>function:</p><ul><li><p>Example:<code>anes20poor_spnd.3) <- c("Increase", "Increase", "Keep Same", "Decrease", "Decrease")</code>.</p></li></ul></li><li><p><strong>Check Transformations</strong>: Use barplot to visualize category distributions post-transformation.</p></li></ul><h4 id="bc981b7b-b1fa-4588-a8e1-0607694f0a6c" data-toc-id="bc981b7b-b1fa-4588-a8e1-0607694f0a6c" collapsed="false" seolevelmigrated="true">Reordering Categories</h4><ul><li><p>To ensure logical ordering of categories (e.g., from low to high), use the <code>ordered</code> function:</p><ul><li><p>Example: <code>anes20poor_spnd.3 <- ordered(anes20poorspnd.3,levels=c("Decrease","KeepSame","Increase"))</code>.</p></li></ul></li></ul><h4id="a50f4e2b−f051−4187−bb8a−577adc646ec5"data−toc−id="a50f4e2b−f051−4187−bb8a−577adc646ec5"collapsed="false"seolevelmigrated="true">CombiningVariables</h4><ul><li><p>Usefultocombineseparatevariablesintooneforanalysis.</p></li><li><p>Forinstance,calculatinganetpartyfeelingthermometerbysubtractingRepublicanratingsfromDemocraticratings:</p><ul><li><p><code>anes20poor_spnd.3, levels=c("Decrease", "Keep Same", "Increase"))</code>.</p></li></ul></li></ul><h4 id="a50f4e2b-f051-4187-bb8a-577adc646ec5" data-toc-id="a50f4e2b-f051-4187-bb8a-577adc646ec5" collapsed="false" seolevelmigrated="true">Combining Variables</h4><ul><li><p>Useful to combine separate variables into one for analysis.</p></li><li><p>For instance, calculating a net party feeling thermometer by subtracting Republican ratings from Democratic ratings:</p><ul><li><p><code>anes20netpty_ft <- (anes20demptyft−anes20dempty_ft - anes20reppty_ft).

    • Positive values indicate a preference for Democrats, negative for Republicans, and zero for neutrality.

    Transforming to Categories

    • To categorize the net party feeling into three groups using cut2:

      • Example:

      • anes20netptyft.3<−ordered(cut2(anes20netpty_ft.3 <- ordered(cut2(anes20netpty_ft, c(0, 1))).

    • Check Class: Ensure the new variable's class is correct after transformation.

    Saving Changes

    • To maintain data integrity, always ensure that original datasets are not overwritten with transformations.

    • Recommended to save as a new file (e.g., anes20a) or overwrite only if new variables were added.

    • Saving Command: save(object, file="<FilePath>/filename").

    4.9 Exercises

    1. Create a new variable combining responses from various gun control measures to form an index.

    2. Create a new ordinal variable based on a pre-existing ordinal scale (party identification) with fewer categories.

    3. Construct a bar chart to visualize the differences between frequency tables of original and modified variables.