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

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

Add a factor level to a table

问题

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

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

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

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

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

英文:

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

&gt; dat &lt;- data.frame(x = c(1, 1, 3))
&gt; table(dat)
x
1 3 
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:

&gt; table(dat)
x
1 2 3 
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

需要明确指定级别,即

table(factor(dat$x, levels = 1:3))

# 1 2 3
# 2 0 1
英文:

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

table(factor(dat$x, levels = 1:3))

# 1 2 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.

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

答案3

得分: 1

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

dat <- data.frame(x = c(1, 1, 3))

dat$x <- factor(dat$x, levels = 1:3)

table(dat)

结果:

x
1 2 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:

dat &lt;- data.frame(x = c(1, 1, 3))

dat$x &lt;- factor(dat$x, levels = 1:3)

table(dat)

Result:

x
1 2 3 
2 0 1 

答案4

得分: 0

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

为了完整起见,使用`data.table`可以这样做。我包括了一个关于数值值以及因子的示例。

dat <- data.frame(
  x = c(1, 1, 3),
  y = factor(c(1, 1, 3), levels = 1:3),
  z = factor(c(1, 1, 3), levels = 1:3, labels = c("cat", "bird", "dog"))
)

# 数值值在列x中的示例
setDT(dat)[.(min(x):max(x)), .N, .EACHI, on = "x"]

#    x N
# 1: 1 2
# 2: 2 0
# 3: 3 1

# 因子列y的示例
setDT(dat)[.(levels(y)), .N, .EACHI, on = "y"]

#    y N
# 1: 1 2
# 2: 2 0
# 3: 3 1

# 与上面相同,带有标签的因子
setDT(dat)[.(levels(z)), .N, .EACHI, on = "z"]

#       z N
# 1:  cat 2
# 2: bird 0
# 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.

dat &lt;- data.frame(
  x = c(1, 1, 3),
  y = factor(c(1, 1, 3), levels = 1:3),
  z = factor(c(1, 1, 3), levels = 1:3, labels = c(&quot;cat&quot;, &quot;bird&quot;, &quot;dog&quot;))
)

# example with numeric values in column x
setDT(dat)[.(min(x):max(x)), .N, .EACHI, on = &quot;x&quot;]

#    x N
# 1: 1 2
# 2: 2 0
# 3: 3 1

# example with factor column y
setDT(dat)[.(levels(y)), .N, .EACHI, on = &quot;y&quot;]

#    y N
# 1: 1 2
# 2: 2 0
# 3: 3 1

# same as above, a factor with labels
setDT(dat)[.(levels(z)), .N, .EACHI, on = &quot;z&quot;]

#       z N
# 1:  cat 2
# 2: bird 0
# 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:

确定