1

我有一个大数据集,其中一个小样本看起来像下面的 4 x 5 小标题。我正在尝试使用如下变量将多个分隔列拆分为唯一行c=="Split"

library(splitstackshape)

dt <- tibble(
a = c("Quartz | White Spirit | Wildfire", "Quiet Riot", "Race Against Time", "Down | Heart Lane | X | Breaking H"),
b = c("Muthas Pride", "Killer Girls / Slick Black Cadillac", "Demo 1980", "Life 55"),
c = c("Split", "Single", "Demo", "Split"),
d = c("Birmingham, England | Hartlepool, England | Sheffield, South Yorkshire, England", "Los Angeles, California", "Nottingham, England", "Liverpool | Beijing | | NYC"),
e = c("wf | ef | ff", "g", "f", "cf | af | df | rf")
)

dt.s <- subset(dt, c == "Split")
dt.split <- cSplit(dt.s, c("a", "d", "e"), c("|", "|", "|"), "long") 
dt.split

但是,这会强制增加一行 NA,如第 4 行所示:

             a            b     c                                   d  e
1:       Quartz Muthas Pride Split                 Birmingham, England wf
2: White Spirit Muthas Pride Split                 Hartlepool, England ef
3:     Wildfire Muthas Pride Split Sheffield, South Yorkshire, England ff
4:           NA Muthas Pride Split                                  NA NA
5:         Down      Life 55 Split                           Liverpool cf
6:   Heart Lane      Life 55 Split                             Beijing af
7:            X      Life 55 Split                                     df
8:   Breaking H      Life 55 Split                                 NYC rf

如果我只拆分两列,这不是问题。如何让它不产生 NA 行?而且,有没有办法在不设置cSplit子集的情况下进行工作c

4

2 回答 2

0

当我们使用 atibble时,我们可以使用separate_rows不给NA出行的

library(tidyr)
separate_rows(dt.s, c('a', "d", "e"), sep="\\s*\\|\\s*") %>%
         select_at(names(dt.s))
# A tibble: 7 x 5
#             a            b     c                                   d     e
#         <chr>        <chr> <chr>                               <chr> <chr>
#1       Quartz Muthas Pride Split                 Birmingham, England    wf
#2 White Spirit Muthas Pride Split                 Hartlepool, England    ef
#3     Wildfire Muthas Pride Split Sheffield, South Yorkshire, England    ff
#4         Down      Life 55 Split                           Liverpool    cf
#5   Heart Lane      Life 55 Split                             Beijing    af
#6            X      Life 55 Split                                        df
#7   Breaking H      Life 55 Split                                 NYC    rf

关于为什么cSplit给出额外的 NA 行,最好检查“宽”格式的输出

cSplit(dt.s, c("a", "d", "e"), "|")
#              b     c    a_1          a_2      a_3        a_4                 d_1                 d_2                                 d_3 d_4 e_1 e_2 e_3 e_4
#1: Muthas Pride Split Quartz White Spirit Wildfire         NA Birmingham, England Hartlepool, England Sheffield, South Yorkshire, England  NA  wf  ef  ff  NA
#2:      Life 55 Split   Down   Heart Lane        X Breaking H           Liverpool             Beijing                                     NYC  cf  af  df  rf

在这里,我们发现对于第二行,分隔符 ie 的数量|为 4,为第一行创建 a NA,因为 'a 列只有 3 个分隔符。因此,当我们使用“长”格式时,该NA行会传播。这可能是一个错误。

于 2017-11-01T03:20:04.690 回答
0

尝试添加makeEqual = FALSE到您的cSplit通话中:

cSplit(dt.s, c("a", "d", "e"), "|", "long", makeEqual = FALSE)
##               a            b     c                                   d  e
## 1:       Quartz Muthas Pride Split                 Birmingham, England wf
## 2: White Spirit Muthas Pride Split                 Hartlepool, England ef
## 3:     Wildfire Muthas Pride Split Sheffield, South Yorkshire, England ff
## 4:         Down      Life 55 Split                           Liverpool cf
## 5:   Heart Lane      Life 55 Split                             Beijing af
## 6:            X      Life 55 Split                                     df
## 7:   Breaking H      Life 55 Split                                 NYC rf

此外,由于您已经在使用“tidyverse”中的包,您可以在拆分的同时进行子集化,如下所示:

dt %>% 
  filter(c == "Split") %>% 
  cSplit(c("a", "d", "e"), "|", "long", makeEqual = FALSE)
于 2018-01-09T10:00:43.840 回答