首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从expss包中添加行

从expss包中添加行
EN

Stack Overflow用户
提问于 2020-08-19 10:03:53
回答 2查看 161关注 0票数 3

我想将特定位置的行添加到expss输出etable中。我使用的是一些蛮力方法,它总是在etable的开头添加行。在特定位置添加行的任何方法。

代码语言:javascript
复制
library(tidyverse)
library(expss)

test1 <-
    mtcars %>% 
    tab_cells(cyl) %>% 
    tab_cols(vs) %>% 
    tab_stat_cpct() %>% 
    tab_pivot()

test1 %>% 
  tibble() %>% 
  tibble::add_row(.data = tibble("", test1[2, -1]/test1[1, -1]*100) %>% 
            set_names(names(test1))
          , .before = 3)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-08-24 22:35:55

基于@caldwellst代码的解决方案,但具有自动比率计算:

代码语言:javascript
复制
insert_ratio <- function(tbl, where) {
    if(is.character(where)) {
        # if where is character we search it in the rowlabels
        where = grep(where, tbl[[1]], fixed = TRUE)[1]
    }
    isTRUE(where>1) || stop("'where' should be greater than 1 for ratio calculation.")
    isTRUE(where<=NROW(tbl)) || stop("'where' should be less or equal than number of rows in the table.")
    tbl1 <- tbl[1:where,]
    to_insert = c(row_labels = tbl[[1]][where], tbl[where, -1]/tbl[where - 1, -1]*100)
    tbl2 <- tbl[(where+1):nrow(tbl),]
    tbl1 %>%
        add_rows(to_insert) %>%
        add_rows(tbl2)
}

insert_ratio(test1, 2)
 # |     |              |    vs |      |
 # |     |              |     0 |    1 |
 # | --- | ------------ | ----- | ---- |
 # | cyl |            4 |   5.6 | 71.4 |
 # |     |            6 |  16.7 | 28.6 |
 # |     |              | 300.0 | 40.0 |
 # |     |            8 |  77.8 |      |
 # |     | #Total cases |  18.0 | 14.0 |

insert_ratio(test1, "cyl|6")
 # the same result

更新比率计算被移动到单独的功能:

代码语言:javascript
复制
ratio = function(tbl, where, label = NULL){
    if(is.character(where)) {
        # if where is character we search it in the rowlabels
        where = grep(where, tbl[[1]], fixed = TRUE)[1]
    }
    isTRUE(where>1) || stop("'where' should be greater than 1 for ratio calculation.")
    isTRUE(where<=NROW(tbl)) || stop("'where' should be less or equal than number of rows in the table.")
    
    if(is.null(label)) label = tbl[[1]][where]
    c(row_labels = label, tbl[where, -1]/tbl[where - 1, -1]*100)
}

insert_row = function(tbl, where, row) {
    if(is.character(where)) {
        # if where is character we search it in the rowlabels
        where = grep(where, tbl[[1]], fixed = TRUE)[1]
    }
    isTRUE(where<=NROW(tbl)) || stop("'where' should be less or equal than number of rows in the table.")
    first_part = seq_len(where)
    tbl1 <- tbl[first_part,]
    
    tbl2 <- tbl[-first_part,]
    tbl1 %>%
        add_rows(row) %>%
        add_rows(tbl2)
}

insert_row(test1, 2, ratio(test1, 2))
insert_row(test1, "cyl|6", ratio(test1, "cyl|6"))
票数 1
EN

Stack Overflow用户

发布于 2020-08-19 12:48:57

不确定是否有一个使用expss导出的简单方法,但我们可以使用带有简单自定义函数的expss::add_rows()来拆分表以实现这一点。

代码语言:javascript
复制
insert_row <- function(tbl, where, ...) {
  args <- c(...)
  tbl1 <- tbl[1:where,]
  tbl2 <- tbl[(where+1):nrow(tbl),]
  tbl1 %>%
    add_rows(args) %>%
    add_rows(tbl2)
}

insert_row(test1, 2, c("cyl|4", 300, 40))

 |     |              |               vs |                  |
 |     |              |                0 |                1 |
 | --- | ------------ | ---------------- | ---------------- |
 | cyl |            4 | 5.55555555555556 | 71.4285714285714 |
 |     |            6 | 16.6666666666667 | 28.5714285714286 |
 |     |            4 |              300 |               40 |
 |     |            8 | 77.7777777777778 |                  |
 |     | #Total cases |               18 |               14 |
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63484614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档