添加一个因子水平到表中。

huangapple go评论82阅读模式
英文:

Add a factor level to a table

问题

我已从数据框中的一列构建了一个列联表:

  1. > dat <- data.frame(x = c(1, 1, 3))
  2. > table(dat)
  3. x
  4. 1 3
  5. 2 1

我想要在表格中添加一个"列"来表示缺失的因子水平(在这种情况下是"2"),并将其计数设置为0。结果应该如下所示:

  1. > table(dat)
  2. x
  3. 1 2 3
  4. 2 0 1

我已经在网站上搜索过,并找到了许多类似的问题,但尽管它们在问题标题和正文中使用了术语"表格",但实际上都是关于数据框的。

英文:

I have built a contingency table from a column in a dataframe:

  1. &gt; dat &lt;- data.frame(x = c(1, 1, 3))
  2. &gt; table(dat)
  3. x
  4. 1 3
  5. 2 1

I want to add a "column" to the table for the missing factor levels (in this case "2") with a count of 0. The result should look like this:

  1. &gt; table(dat)
  2. x
  3. 1 2 3
  4. 2 0 1

I have searched the site and found many similar questions, but while they use the term "table" in the question title and body, they all actually ask about dataframes.

答案1

得分: 2

需要明确指定级别,即

  1. table(factor(dat$x, levels = 1:3))
  2. # 1 2 3
  3. # 2 0 1
英文:

You need to explicitly specify the levels, i.e.

  1. table(factor(dat$x, levels = 1:3))
  2. # 1 2 3
  3. # 2 0 1

答案2

得分: 1

tabulate(dat$x)

[1] 2 0 1

英文:

Since you seem to look for how the integer bins 1:max are filled, you could use tabulate instead.

  1. tabulate(dat$x)
  2. # [1] 2 0 1

答案3

得分: 1

当您创建一个因子变量时,您可以明确列出所有水平,即使数据中没有出现的水平:

  1. dat <- data.frame(x = c(1, 1, 3))
  2. dat$x <- factor(dat$x, levels = 1:3)
  3. table(dat)

结果:

  1. x
  2. 1 2 3
  3. 2 0 1
英文:

When you create a factor variable, you can explicit all levels even the ones that do not appear in the data:

  1. dat &lt;- data.frame(x = c(1, 1, 3))
  2. dat$x &lt;- factor(dat$x, levels = 1:3)
  3. table(dat)

Result:

  1. x
  2. 1 2 3
  3. 2 0 1

答案4

得分: 0

以下是您要翻译的代码部分:

  1. 为了完整起见,使用`data.table`可以这样做。我包括了一个关于数值值以及因子的示例。
  2. dat <- data.frame(
  3. x = c(1, 1, 3),
  4. y = factor(c(1, 1, 3), levels = 1:3),
  5. z = factor(c(1, 1, 3), levels = 1:3, labels = c("cat", "bird", "dog"))
  6. )
  7. # 数值值在列x中的示例
  8. setDT(dat)[.(min(x):max(x)), .N, .EACHI, on = "x"]
  9. # x N
  10. # 1: 1 2
  11. # 2: 2 0
  12. # 3: 3 1
  13. # 因子列y的示例
  14. setDT(dat)[.(levels(y)), .N, .EACHI, on = "y"]
  15. # y N
  16. # 1: 1 2
  17. # 2: 2 0
  18. # 3: 3 1
  19. # 与上面相同,带有标签的因子
  20. setDT(dat)[.(levels(z)), .N, .EACHI, on = "z"]
  21. # z N
  22. # 1: cat 2
  23. # 2: bird 0
  24. # 3: dog 1
英文:

For completeness, using data.table one could do it like this. I included an example on numeric values as well as factors.

  1. dat &lt;- data.frame(
  2. x = c(1, 1, 3),
  3. y = factor(c(1, 1, 3), levels = 1:3),
  4. z = factor(c(1, 1, 3), levels = 1:3, labels = c(&quot;cat&quot;, &quot;bird&quot;, &quot;dog&quot;))
  5. )
  6. # example with numeric values in column x
  7. setDT(dat)[.(min(x):max(x)), .N, .EACHI, on = &quot;x&quot;]
  8. # x N
  9. # 1: 1 2
  10. # 2: 2 0
  11. # 3: 3 1
  12. # example with factor column y
  13. setDT(dat)[.(levels(y)), .N, .EACHI, on = &quot;y&quot;]
  14. # y N
  15. # 1: 1 2
  16. # 2: 2 0
  17. # 3: 3 1
  18. # same as above, a factor with labels
  19. setDT(dat)[.(levels(z)), .N, .EACHI, on = &quot;z&quot;]
  20. # z N
  21. # 1: cat 2
  22. # 2: bird 0
  23. # 3: dog 1

huangapple
  • 本文由 发表于 2023年3月7日 16:11:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659402.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定