这是对此问题的后续问题。
改写:
在之前的数据集的基础上,name
现在对我的汇总统计数据很重要,因为有许多不同名称的动物。相反,我们仍然可以在该key_quality
领域进行描述性统计。
您会看到生成的表格包含所有单独的统计数据,但我也希望看到所有幼犬身高的平均值。然后我也想看看所有哺乳动物婴儿的平均身高。我们如何添加这些统计数据?
animals_3 <- data.table(
family = rep(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 , 2, 2, 2, 2,3 ,3 ,3), 2),
animal = rep(c(1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4), 2),
name = rep(c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6),2),
key_quality = rep(c(rep("bold", 3), rep("smiles", 3), rep("sings", 3), rep("fast", 3), rep("sleepy", 3), rep("ticklish", 3)), 2),
age = rep(c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3), 2),
field = c(rep(1, 18), rep(2, 18)),
value = c(
c(25, 45, 75, 40, 65, 80, 10, 25, 50, 10, 15, 25, 18, 24, 40, 5, 15, 20),
c(5, 15, 30, 7, 19, 40, 3, 9, 13, 2, 5, 9, 4, 7, 14, 1, 2, 3.5))
)
animals_3 <- expss::apply_labels(
animals_3,
family = "|",
family = c("mammal" = 1, "reptilia" = 2, "amphibia" = 3),
animal = "|",
animal = c("dog" = 1, "cat" = 2, "turtle" = 3, "frog" = 4),
name = "|",
name = c("fred" = 1, "billiy" = 2, "tod" = 3, "timmy" = 4, "ricardo" = 5, "jonno" = 6),
key_quality = "|",
age = "|",
age = c("baby" = 1, "young" = 2, "mature" = 3),
field = "|",
field = c("height" = 1, "weight" = 2),
value = "|"
)
expss::expss_output_viewer()
animals_3 %>%
expss::tab_rows(family %nest% animal %nest% name) %>%
# here we create separate column for name
expss::tab_cols(total(label = "quality")) %>%
expss::tab_cells(key_quality) %>%
expss::tab_stat_fun(unique) %>%
# end of creation
expss::tab_cells(value) %>%
expss::tab_cols(age %nest%field) %>%
expss::tab_stat_sum(label = "") %>%
expss::tab_pivot(stat_position = "outside_column") %>%
expss::drop_empty_rows()
非常感谢!