r - 有没有办法用 R 中的列拆分并估算隐含值

标签 r dplyr tidyr

我正在尝试拆分数据集中的一列,该列的代码由“-”分隔。这产生了两个问题。首先,我必须拆分列,但我也想估算“-”隐含的值。我能够使用以下方法拆分数据:

separate_rows(df, code, sep = "-")

但我仍然没有找到一种方法来估算隐含值(value)。

name <- c('group1', 'group1','group1','group2', 'group1', 'group1', 
'group1')
code <- c('93790', '98960 - 98962', '98966 - 98969', '99078', 'S5950', 
'99241 - 99245', '99247')
df <- data.frame( name, code)

我尝试输出的内容看起来像这样:

group1 93790, 98960, 98961, 98962, 98966, 98967, 98968, 98969, S5950, 99241, 
99242, 99243, 99244, 99245, 99247
group2 99078

在此示例中,98961、98967 和 98968 是从“-”推算和暗示的。

关于如何实现这一点有什么想法吗?

最佳答案

在我们拆分'code'之后,一个选项是使用map循环遍历拆分元素,得到一个序列(:),unnest 并执行 group_by paste

library(dplyr)
library(stringr)
library(tidyr)
library(purrr)
df %>% 
  mutate(code = map(strsplit(as.character(code), " - "), ~  {
      x <- as.numeric(.x)
      if(length(x) > 1)  x[1]:x[2] else x})) %>%
  unnest(code) %>%
  group_by(name) %>%
  summarise(code = str_c(code, collapse=", "))
# A tibble: 2 x 2
#   name   code                                                  
#   <fct>  <chr>                                                  
# 1 group1 93790, 98960, 98961, 98962, 98966, 98967, 98968, 98969
# 2 group2 99078        

或者另一个选项是在 separate_rows 之前,创建一个行索引并在我们执行 complete 时使用它进行分组

df %>% 
    mutate(rn = row_number()) %>%
    separate_rows(code, convert = TRUE) %>% 
    group_by(rn, name) %>%
    complete(code = min(code):max(code)) %>%
    group_by(name) %>%
    summarise(code = str_c(code, collapse =", "))

更新

如果有非数字元素

df %>% 
 mutate(rn = row_number()) %>%
 separate_rows(code, convert = TRUE) %>%
 group_by(name, rn) %>% 
 complete(code = if(any(str_detect(code, '\\D'))) code else 
     as.character(min(as.numeric(code)):max(as.numeric(code)))) %>% 
 group_by(name) %>%
 summarise(code = str_c(code, collapse =", "))
# A tibble: 2 x 2
#  name   code                                                                                                   
#  <fct>  <chr>                                                                                                  
#1 group1 93790, 98960, 98961, 98962, 98966, 98967, 98968, 98969, S5950, 99241, 99242, 99243, 99244, 99245, 99247
#2 group2 99078                 

关于r - 有没有办法用 R 中的列拆分并估算隐含值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59271579/

相关文章:

使用 nls 再现 PROC NLIN 输出

r - dplyr group_by - 混合带或不带引号的变量名称

r - 使用 dplyr 一次生成多列

r - 将 top_n 函数映射到分组数据

r - 在 Jupyter 中安装 R 包

按组返回日期范围

r - R 中 rbind() 和 bind_rows() 之间的区别

r - 根据 R 中的其他变量更改值

r - 基于一个列模态和其他列的新列

r - Pivot_wider/spread 而不是 value_from 或值只是 1?