如何删除空行并添加描述性列?
How do you remove empty rows and add descriptive columns?
的后续问题
一旦我在 table 中引入了更多的复杂性,我就会看到不存在组-子组组合的空行。那些可以删除吗?
我还想添加一个 "descriptive" 列,它不适合单元格-行-列制表,我可以这样做吗?
这是一个例子:
animals_2 <- data.table(
family = rep(c(1, 1, 1, 1, 1, 1, 2, 2 ,2 ,3 ,3 ,3), 2),
animal = rep(c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4), 2),
name = rep(c(rep("fred", 3), rep("tod", 3), rep("timmy", 3), rep("johnno", 3)), 2),
age = rep(c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3), 2),
field = c(rep(1, 12), rep(2, 12)),
value = c(c(25, 45, 75, 10, 25, 50, 10, 15, 25, 5, 15, 20), c(5, 15, 30, 3, 9, 13, 2, 5, 9, 1, 2, 3.5))
)
animals_2 <- expss::apply_labels(
animals_2,
family = "|",
family = c("mammal" = 1, "reptilia" = 2, "amphibia" = 3),
animal = "|",
animal = c("dog" = 1, "cat" = 2, "turtle" = 3, "frog" = 4),
name = "|",
age = "age",
age = c("baby" = 1, "young" = 2, "mature" = 3),
field = "|",
field = c("height" = 1, "weight" = 2),
value = "|"
)
expss::expss_output_viewer()
animals_2 %>%
expss::tab_cells(value) %>%
expss::tab_cols(age %nest% field) %>%
expss::tab_rows(family %nest% animal) %>%
expss::tab_stat_sum(label = "") %>%
expss::tab_pivot()
您会看到 "name" 列当前不在 table 中。我只想将它放在每只动物旁边和 Age/Field 摘要之前。这可能吗?
提前致谢!
至于空类别 - 有一个特殊的功能 - 'drop_empty_rows':
library(expss)
animals_2 <- data.table(
family = rep(c(1, 1, 1, 1, 1, 1, 2, 2 ,2 ,3 ,3 ,3), 2),
animal = rep(c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4), 2),
name = rep(c(rep("fred", 3), rep("tod", 3), rep("timmy", 3), rep("johnno", 3)), 2),
age = rep(c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3), 2),
field = c(rep(1, 12), rep(2, 12)),
value = c(c(25, 45, 75, 10, 25, 50, 10, 15, 25, 5, 15, 20), c(5, 15, 30, 3, 9, 13, 2, 5, 9, 1, 2, 3.5))
)
animals_2 <- expss::apply_labels(
animals_2,
family = "|",
family = c("mammal" = 1, "reptilia" = 2, "amphibia" = 3),
animal = "|",
animal = c("dog" = 1, "cat" = 2, "turtle" = 3, "frog" = 4),
name = "|",
age = "age",
age = c("baby" = 1, "young" = 2, "mature" = 3),
field = "|",
field = c("height" = 1, "weight" = 2),
value = "|"
)
expss::expss_output_viewer()
animals_2 %>%
expss::tab_cells(value) %>%
expss::tab_cols(age %nest% field) %>%
expss::tab_rows(family %nest% animal %nest% name) %>%
expss::tab_stat_sum(label = "") %>%
expss::tab_pivot() %>%
drop_empty_rows()
至于列 "name" - 您可以使用管道分隔符将名称添加到值标签:dog|fred' or as in the example above, via
%nest%`。
更新:
如果您需要它作为带标题的列,那么最好将名称作为统计信息放置:
animals_2 %>%
expss::tab_rows(family %nest% animal) %>%
# here we create separate column for name
expss::tab_cols(total(label = "name")) %>%
expss::tab_cells(name) %>%
expss::tab_stat_fun(unique) %>%
# end of creation
expss::tab_cols(age %nest% field) %>%
expss::tab_cells(value) %>%
expss::tab_stat_sum(label = "") %>%
expss::tab_pivot(stat_position = "outside_columns") %>%
drop_empty_rows()
一旦我在 table 中引入了更多的复杂性,我就会看到不存在组-子组组合的空行。那些可以删除吗?
我还想添加一个 "descriptive" 列,它不适合单元格-行-列制表,我可以这样做吗?
这是一个例子:
animals_2 <- data.table(
family = rep(c(1, 1, 1, 1, 1, 1, 2, 2 ,2 ,3 ,3 ,3), 2),
animal = rep(c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4), 2),
name = rep(c(rep("fred", 3), rep("tod", 3), rep("timmy", 3), rep("johnno", 3)), 2),
age = rep(c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3), 2),
field = c(rep(1, 12), rep(2, 12)),
value = c(c(25, 45, 75, 10, 25, 50, 10, 15, 25, 5, 15, 20), c(5, 15, 30, 3, 9, 13, 2, 5, 9, 1, 2, 3.5))
)
animals_2 <- expss::apply_labels(
animals_2,
family = "|",
family = c("mammal" = 1, "reptilia" = 2, "amphibia" = 3),
animal = "|",
animal = c("dog" = 1, "cat" = 2, "turtle" = 3, "frog" = 4),
name = "|",
age = "age",
age = c("baby" = 1, "young" = 2, "mature" = 3),
field = "|",
field = c("height" = 1, "weight" = 2),
value = "|"
)
expss::expss_output_viewer()
animals_2 %>%
expss::tab_cells(value) %>%
expss::tab_cols(age %nest% field) %>%
expss::tab_rows(family %nest% animal) %>%
expss::tab_stat_sum(label = "") %>%
expss::tab_pivot()
您会看到 "name" 列当前不在 table 中。我只想将它放在每只动物旁边和 Age/Field 摘要之前。这可能吗?
提前致谢!
至于空类别 - 有一个特殊的功能 - 'drop_empty_rows':
library(expss)
animals_2 <- data.table(
family = rep(c(1, 1, 1, 1, 1, 1, 2, 2 ,2 ,3 ,3 ,3), 2),
animal = rep(c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4), 2),
name = rep(c(rep("fred", 3), rep("tod", 3), rep("timmy", 3), rep("johnno", 3)), 2),
age = rep(c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3), 2),
field = c(rep(1, 12), rep(2, 12)),
value = c(c(25, 45, 75, 10, 25, 50, 10, 15, 25, 5, 15, 20), c(5, 15, 30, 3, 9, 13, 2, 5, 9, 1, 2, 3.5))
)
animals_2 <- expss::apply_labels(
animals_2,
family = "|",
family = c("mammal" = 1, "reptilia" = 2, "amphibia" = 3),
animal = "|",
animal = c("dog" = 1, "cat" = 2, "turtle" = 3, "frog" = 4),
name = "|",
age = "age",
age = c("baby" = 1, "young" = 2, "mature" = 3),
field = "|",
field = c("height" = 1, "weight" = 2),
value = "|"
)
expss::expss_output_viewer()
animals_2 %>%
expss::tab_cells(value) %>%
expss::tab_cols(age %nest% field) %>%
expss::tab_rows(family %nest% animal %nest% name) %>%
expss::tab_stat_sum(label = "") %>%
expss::tab_pivot() %>%
drop_empty_rows()
至于列 "name" - 您可以使用管道分隔符将名称添加到值标签:dog|fred' or as in the example above, via
%nest%`。
更新: 如果您需要它作为带标题的列,那么最好将名称作为统计信息放置:
animals_2 %>%
expss::tab_rows(family %nest% animal) %>%
# here we create separate column for name
expss::tab_cols(total(label = "name")) %>%
expss::tab_cells(name) %>%
expss::tab_stat_fun(unique) %>%
# end of creation
expss::tab_cols(age %nest% field) %>%
expss::tab_cells(value) %>%
expss::tab_stat_sum(label = "") %>%
expss::tab_pivot(stat_position = "outside_columns") %>%
drop_empty_rows()