-1

I need to compare questions of two different surveys (t1, t2). Therefore, I have two dataframes like those below:

t1     t2

x       x
x       y
y       z
z       w 
y       z 
x       x
z       y
z       w 
w       x
        z 
        v 

This data needs to be grouped by v, w, x, y and z. Unfortunately, value v does not occur in the first dataframe and both dataframes have a diffent amount of rows, so I cannot put them together in one dataframe. When I use "group_by" and "summarise", I get two columns, but one with 4 and the one with 5 rows. Same like before, I cannot put them together.

I don't want to add an additional row in the first dataframe, as I don't want to manipulate the origin dataset.

At the end I need a table, which should look like the following one:

        t1      t2

v       0       1
w       1       2
x       3       3
y       2       2
z       3       3 

I hope you can help me! Thank you!

h96
  • 1

1 Answers1

2

One way would be:

library(tidyverse)

bind_rows(
  gather(t1),
  gather(t2)
) %>% {table(.$val, .$key)}

Output:

    t1 t2
  v  0  1
  w  1  2
  x  3  3
  y  2  2
  z  3  3
arg0naut91
  • 14,574
  • 2
  • 17
  • 38