r语言 - 如何折叠和聚合带有字符/长字符串数据的行



我正在处理一个大型数据集,其中包含大量需要整合的文本数据。据说有独特的案例/观察,但它们确实有重复。问题是,有时重复案例提供了补充和新的信息。因此,我想根据标准折叠/合并案例。

我这里有一个非常小的示例数据集,它说明了这个想法。请注意,实际上varText通常超过 1000 个字符。

varID表示目标唯一观测值

varCat代表一个分类数据,有时它包含 NA,有时它补充了一个观察(在实际情况下,我有大约 10 个(

    varID <- c('a', 'b', 'c', 'd', 'e', 'a', 'b', 'c', 'd', 'c', 'd', 'e', 'a', 'z')
    varText <- c('This is a long text', 'This is also a long text', 
                 'This is short', 'This is another unique long text', 
                 'Blabla1', 'Blabla2', 'Blabla3', 'Blabla4', 'Blabla5', 'Blabla6', 'Blabla7', 
                 'Blabla8', 'This is also a long blabla', 'This case is perfectly fine')
    varCat <- c('CatA', 'CatB', NA, 'CatC', 'CatA', NA, NA, 'CatC', 'CatA', 'CatB', NA, 'CatC', NA, 'CatF')
    df <- data.frame(varID, varText, varCat, stringsAsFactors = FALSE)

示例 df:

       varID                          varText varCat
    1      a              This is a long text   CatA
    2      b         This is also a long text   CatB
    3      c                    This is short   <NA>
    4      d This is another unique long text   CatC
    5      e                          Blabla1   CatA
    6      a                          Blabla2   <NA>
    7      b                          Blabla3   <NA>
    8      c                          Blabla4   CatC
    9      d                          Blabla5   CatA
    10     c                          Blabla6   CatB
    11     d                          Blabla7   <NA>
    12     e                          Blabla8   CatC
    13     a       This is also a long blabla   <NA>
    14     z      This case is perfectly fine   CatF

首先,我确定所有有重复项的情况:

df <- df %>% add_count(varID, name = 'dupe_varID')

然后我还想根据文本的长度来比较文本:

df$text_length <- stringr::str_length(df$varText)

最后,我创建一个只有重复案例的新数据帧。我想我可以使用dplyr group_by.但我不知道如何从这里开始。

# filter all duplicated cases into new df sort ???
df2 <- df %>% filter(dupe_varID > 1) %>% group_by(varID) %>% arrange(desc(text_length), varCat) 

我希望得到以下结果:

  • 应保留最长的varText
  • NA 值
  • 将替换为非 NA 值
  • 删除重复项
  • 如果varCat存在冲突,则文本最长的情况提供varCat
 1 a     This is also a long blabla       CatA
 2 b     This is also a long text         CatB
 3 c     This is short                    CatC
 4 d     This is another unique long text CatC
 5 e     Blabla1                          CatA
14 z     This case is perfectly fine      CatF

一个选项是按"varID"分组,然后将NA元素与相邻的非NA元素fill,并在"varText"中slice具有max个字符数(nchar(的行

library(dplyr)
library(tidyr)
df %>% 
   group_by(varID) %>%
   fill(varCat, .direction = 'downup') %>% 
   slice(which.max(nchar(varText)))
# A tibble: 6 x 3
# Groups:   varID [6]
#  varID varText                          varCat
#  <chr> <chr>                            <chr> 
#1 a     This is also a long blabla       CatA  
#2 b     This is also a long text         CatB  
#3 c     This is short                    CatC  
#4 d     This is another unique long text CatC  
#5 e     Blabla1                          CatA  
#6 z     This case is perfectly fine      CatF  

相关内容

  • 没有找到相关文章

最新更新