D. Studer
D. Studer

Reputation: 1875

restructure dataframe (pivot_wider)

I have again some problems with a data.frame I need to restructure:

My dataframe looks as follows. It describes three datasets (1-3) and the column names these three datasets contain:

df <- data.frame(df =   c(1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3),
                 vars = c("var1", "var2", "var3", "var4", 
                          "var1", "var1b", "var4", 
                          "var5", "var1", "var2", "var7"))

   df  vars
1   1  var1
2   1  var2
3   1  var3
4   1  var4
5   2  var1
6   2 var1b
7   2  var4
8   3  var5
9   3  var1
10  3  var2
11  3  var7


My goal is to have an overview table like this (the values are not exactly the ones from above), where 1 means that the variable is included and 2 means the variable is not included in the corresponding dataframe df1 to df3:

df %>% pivot_wider(names_from = df, values_from = ??)
          df1          df2          df3
_________________________________________________________________
var1        1            0            0
var1b       1            1            0
var2        1            1            1
var3        0            1            1
var4        0            0            1
var7        0            0            1

Upvotes: 0

Views: 65

Answers (2)

Onyambu
Onyambu

Reputation: 79228

Just do:

t(table(df))

or even

 table(rev(df))

or even

xtabs(~vars+df, df)

if you need a dataframe back:

as.data.frame.matrix(t(table(df)))
      1 2 3
var1  1 1 1
var1b 0 1 0
var2  1 0 1
var3  1 0 0
var4  1 1 0
var5  0 0 1
var7  0 0 1

Upvotes: 5

Karthik S
Karthik S

Reputation: 11584

Does this work:

library(dplyr)
library(tidyr)
library(tibble)
df %>% group_by(df, vars) %>% 
  mutate(c = n()) %>% 
  pivot_wider(id_cols = vars, names_from = df, names_prefix = 'df', values_from = c, values_fill = 0) %>% 
  column_to_rownames('vars')
      df1 df2 df3
var1    1   1   1
var2    1   0   1
var3    1   0   0
var4    1   1   0
var1b   0   1   0
var5    0   0   1
var7    0   0   1
> 

Upvotes: 3

Related Questions