Search code examples
rmergecbind

Combining Tables In R


UPDATED

I encountered a problem while trying to perform a simple table merging task in R. I'm looking for why this happened and, if it exists, a more elegant solution. Below is the exact data I'm working with and what happened.

I have two tables from a survey related to how different political parties and business people think about a political issue. They come from two different datasets, which I don't want to merge (for various reasons). Each data has the exact same names for values. But, when I used cbind to merge the columns, the rows get reverse for some columns and I can't figure out why.

My data

library(survey)
founders.services <- 
structure(c(38, 43, 131, 172, 177, 122, 34, 12, 114, 70, 17, 
27), .Dim = c(2L, 6L), .Dimnames = structure(list(services = c("compete", 
"similar"), party = c("skipped", "Democrat", "Independent", "Libertarian", 
"Republican", "other")), .Names = c("services", "party")))

public.services.party <- 
structure(c(26, 103), .Dim = 2L, .Dimnames = structure(list(services = c("similar", 
"compete")), .Names = "services"), class = c("svytable", "xtabs", 
"table"), call = svytable.survey.design(formula = ~services, 
    design = fss))

And, how I combine them:

cbind(founders.services, public.services.party)

In the first (and correct) table, under the column "libertarian", the row "compete" has the value of 34 and "similar" has 12. But, in the combined table (with cbind), it's the reverse. If the names were different, they should show up as different columns. But, cbind seems to recognize that they are both the same values.

Why is this happening?

And, more generally, if there's a better way to combine tables, I'm happy to consider alternatives. Basically what I have is a few different data sets where various populations (political party, business type) answered the same questions. I can't merge the datasets, but would like to combine the tables for analysis.

Thanks and please let me know if I can make this question any more clear.

UPDATED: with code and tables.

Here is the correct table

services  skipped Democrat Independent Libertarian Republican other
  compete      38      131         177          34        114    17
  similar      43      172         122          12         70    27

Here is the combined table with error. you will need the "survey" package to replicate.

        founders.services skipped Democrat Independent Libertarian Republican other
similar                26      38      131         177          34        114    17
compete               103      43      172         122          12         70    27

Solution

  • It is taking the row names from the first data frame you put in the cbind command. If you just reverse the order of the cbind, you will get what you want:

    > cbind( public.services.party,founders.services)
            skipped Democrat Independent Libertarian Republican other founders.services
    compete      38      131         177          34        114    17                26
    similar      43      172         122          12         70    27               103
    

    You can reorder columns and rows as you wish afterwards.