英文:
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:
> dat <- data.frame(x = c(1, 1, 3))
> 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:
> 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 <- data.frame(x = c(1, 1, 3))
dat$x <- 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 <- 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"))
)
# example with numeric values in column x
setDT(dat)[.(min(x):max(x)), .N, .EACHI, on = "x"]
# x N
# 1: 1 2
# 2: 2 0
# 3: 3 1
# example with factor column y
setDT(dat)[.(levels(y)), .N, .EACHI, on = "y"]
# 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 = "z"]
# z N
# 1: cat 2
# 2: bird 0
# 3: dog 1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论