英文:
Converting a table()-output to a matrix (or dataframe)
问题
使用以下代码将结果转换为与上面输出相同的数据框或矩阵:
result <- table(df$Factor1, df$Factor2)
result_df <- as.data.frame.matrix(result)
希望这有帮助!
英文:
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!
答案1
得分: 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
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论