0

With

df <- data.frame(Factor1 = factor(c(rep ("A", 2), rep ("B", 2), rep("C", 2))),
                 Factor2 = factor(LETTERS[12:7]))

table(df$Factor1, df$Factor2)

how to convert the result:

    G H I J K L
  A 0 0 0 0 1 1
  B 0 0 1 1 0 0
  C 1 1 0 0 0 0

into a dataframe or matrix that looks exactly like the output above?

Thanks!

nouse
  • 3,315
  • 2
  • 29
  • 56

1 Answers1

2
df <- data.frame(Factor1 = factor(c(rep("A", 2), rep("B", 2), rep("C", 2))),
                 Factor2 = factor(LETTERS[12:7]))

result <- table(df$Factor1, df$Factor2)
result_df <- as.data.frame.matrix(result)

print(result_df)

result:

  G H I J K L
A 0 0 0 0 1 1
B 0 0 1 1 0 0
C 1 1 0 0 0 0
szmple
  • 453
  • 1
  • 8