R 中多列的值计数

kio*_*oti 2 r count

我有一个数据集,其中一组值分散在多个列中:

ID 优先级1 优先级2 优先级3
2308 写作 阅读 阅读
第0329章 阅读 沟通 写作
2389 沟通 写作 沟通
2934 写作 沟通 写作

我希望输出是一个表,其中第一列是找到的唯一值(写作、阅读、交流),其余列是优先级(优先级 1、优先级 2、优先级 3)。每列中应该是该实例中的优先级计数。输出应如下所示:

优先类型 优先级1 优先级2 优先级3
写作 2 1 2
阅读 1 1 1
沟通 1 2 1

在我的实际数据集中,有很多优先级,因此如果可能的话我们可以在列中包含 1:n 吗?

先感谢您。

*编辑表已更新为新列以进行澄清。我想完全忽略 ID 列,只计算每个优先级列中的优先级。

Ony*_*mbu 5

table(stack(df))

               ind
values          Priority 1 Priority 2 Priority 3
  Communication          1          2          1
  Reading                1          1          1
  Writing                2          1          2
Run Code Online (Sandbox Code Playgroud)

如果你想把它作为数据框:

as.data.frame.matrix(table(stack(df)))
              Priority 1 Priority 2 Priority 3
Communication          1          2          1
Reading                1          1          1
Writing                2          1          2
Run Code Online (Sandbox Code Playgroud)