R将整洁的分层数据帧转换为分层列表

R将整洁的分层数据帧转换为分层列表

本文介绍了R将整洁的分层数据帧转换为分层列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

转换此

g1    g2    desc    val
A     a     1       v1
A     a     2       v2
A     b     3       v3

收件人:

desc    val
A
a
1       v1
2       v2
b
3       v3

我已经使用for循环将具有两个分组级别的分层数据帧转换为结构化列表.这样会显示描述,并在列表中按顺序插入相关变量,并依次插入组级别.

I've converted a hierarchical data frame with two grouping levels into a structured list using a for loop. This displayed descriptions with an associated variable in a list interspersed with the group levels in order.

目的是将分层数据显示为列表,以便可以使用openxlsx进行格式化以区分不同的分组级别.

The purpose is to present the hierarchical data as a list so that it can be printed with formatting to distinguish the different grouping levels, using openxlsx.

是否有更有效的基数R,tidyverse或其他方法来实现这一目标?

Is there a more efficient base R, tidyverse or other approach to achieve this?

用于循环代码

tib <-  tibble(g1 = c("A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "C"),
          g2 = c("a", "a", "b", "b", "b", "c", "d", "d", "b", "b", "e", "e"),
          desc = 1:12,
          val = paste0("v", 1:12))

# Number of rows in final table
n_rows <- length(unique(tib$g1)) + length(unique(paste0(tib$g1, tib$g2))) + nrow(tib)

# create empty output tibble
output <-
    as_tibble(matrix(nrow = n_rows, ncol = 2)) %>%
    rename(desc = V1, val = V2) %>%
    mutate(desc = NA_character_,
           val = NA_real_)

# loop counters
level_1 <- 0
level_2 <- 0
output_row <- 1

for(i in seq_len(nrow(tib))){

  # level 1 headings
  if(tib$g1[[i]] != level_1) {
    output$desc[[output_row]] <- tib$g1[[i]]
    output_row <- output_row + 1
    }

  # level 2 headings
  if(paste0(tib$g1[[i]], tib$g2[[i]]) != paste0(level_1, level_2)) {
    output$desc[[output_row]] <- tib$g2[[i]]
    output_row <- output_row + 1
  }

  level_1 <- tib$g1[[i]]
  level_2 <- tib$g2[[i]]

  # Description and data
  output$desc[[output_row]] <- tib$desc[[i]]
  output$val[[output_row]] <- tib$val[[i]]
  output_row <- output_row + 1

}

推荐答案

使用tidyverse中的一些软件包,我们可以做到:

Using a few packages from the tidyverse, we could do:

library(tidyverse)

# or explicitly load what you need
library(purrr)
library(dplyr)
library(tidyr)
library(stringr)

transpose(df) %>%
  unlist() %>%
  stack() %>%
  distinct(values, ind) %>%
  mutate(detect_var = str_detect(values, "^v"),
         ind = lead(case_when(detect_var == TRUE ~ values)),
         values = case_when(detect_var == TRUE ~ NA_character_,
                            TRUE ~ values)) %>%
  drop_na(values) %>%
  select(values, ind) %>%
  replace_na(list(ind = ""))

返回:

  values ind
1      A
2      a
3      1  v1
5      2  v2
7      b
8      3  v3

使用tib数据集,我的解决方案似乎比Plamen的解决方案慢一点:

Using tib data set, my solution seems to be a little slower than Plamen's:

Unit: milliseconds
       expr       min        lq      mean    median        uq        max neval
        old 17.658398 18.492957 21.292965 19.396304 21.770249 133.215223   100
 new_simple  6.742158  7.013732  7.638155  7.190095  7.759104  12.640237   100
   new_fast  4.064907  4.266243  4.837131  4.507865  4.871533   9.442904   100
  tidyverse  4.980664  5.326694  6.004602  5.552611  6.215129   9.923524   100

这篇关于R将整洁的分层数据帧转换为分层列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:32