将不均匀的字符串拆分为R中的排序列

时间:2019-06-27 14:12:29

标签: r sorting unique strsplit grepl

进行了一项调查,其中一个问题可以选择多个答案。选择多个答案时,它们都记录在同一单元格中。

此外,每个调查员在单元格中记录此信息的方式也有所不同。有时,分隔符是连字符(-),有时是斜杠(/)。还有一些测量师用数字列出了这些项目。

一个例子是房屋中的物品清单(见下图/图片)。我想在每个项目可用时创建列(新列可能具有1/0或项目名称/ NA)(请参见下面的结果示例)。

我可以使用文本到列和查找数组在excel中执行此操作,但是在同一列中有很多excel工作表,因此我必须在R中执行此操作。对不起,我不知道如何制作示例表使用R代码,但希望有人能够提供帮助。

这是数据的样子:

House = c("h1","h2","h3","h4","h5","h6","h7","h8","h9","h10","h11")
Items = c("Chair", "Chair- Window/Glass- "," Door- Sofa-", "Chair- 
Window/Glass Frame- ", "1. Window/Glass Frame", "Chair- Door- Window-", "Chair- Sofa - Door- Table-", " 4. Table", "Couch (2)", "Window- Table- Chair- Sofa- Door- Couach", "2. Door / Chair")
table1 = as.data.table(House)
table2 = as.data.table(Items)
table = as.data.frame(append(table1, table2))

table

+-------+------------------------------------------+
| House |                  Items                   |
+-------+------------------------------------------+
|   001 | Chair                                    |
|   002 | Chair- Window/Glass-                     |
|   003 | Door- Sofa-                              |
|   004 | Chair- Window/Glass Frame-               |
|   005 | 1. Window/Glass Frame                    |
|   006 | Chair- Door- Window-                     |
|   007 | Chair- Sofa - Door- Table-               |
|   008 | 4. Table                                 |
|   009 | Couch (2)                                |
|   010 | Window- Table- Chair- Sofa- Door- Couach |
|   011 | 2. Door / Chair                          |
+-------+------------------------------------------+

我的想法是使用所有定界符(strsplit)进行拆分,删​​除空格(trimws),获得唯一列表(唯一),然后将所有变体替换为我想要的标准(grepl),最后将它们放入根据类别。

items <- strsplit(df$Items, "[/.-]")
items <- trimws(items)
items <- df$Items %>%
    strsplit("[/.-]") %>%
    str_trim(side = "both")
items_list <- unique(items)

这就是我想要得到的: (窗户和玻璃是相同的,椅子/沙发/沙发是相同的,依此类推-因此,我只需要创建更大的类别,而不用创建几列基本上相同的东西)

Outcome

+-------+-------+--------+-------+------+
| House | Chair | Window | Table | Door |
+-------+-------+--------+-------+------+
|   001 | Chair |        |       |      |
|   002 | Chair | Window |       |      |
|   003 | Chair |        |       | Door |
|   004 | Chair | Window |       |      |
|   005 |       | Window |       |      |
|   006 | Chair | Window |       | Door |
|   007 | Chair |        | Table | Door |
|   008 |       |        | Table |      |
|   009 | Chair |        |       |      |
|   010 | Chair | Window | Table | Door |
|   011 | Chair |        |       | Door |
+-------+-------+--------+-------+------+

1 个答案:

答案 0 :(得分:2)

您可以在str_detect(或grepl)中使用map_df(或sapply)来生成逻辑数据框,将其强制为整数0/1,然后将其绑定到您的原始数据框。此方法绕开了拆分/清洁/麻烦的麻烦。数据。它只需要您首先为正则表达式创建模式组,即chair|sofa|couach|couchwindow|glass

library(stringr)
library(dplyr)
library(purrr)

# Create regex pattern groups.
patts <- c(chair = "chair|sofa|couach|couch", window = "window|glass", 
           table = "table", door = "door")

# Detect pattern groups, coerce to 0/1, bind to origional dataframe.
map_df(patts, ~ str_detect(df$Items, regex(., ignore_case = T))) %>%
    mutate_all(as.integer) %>% 
    bind_cols(df, .)

这将返回以下数据帧:

# A tibble: 11 x 6
   House Items                                    chair window table  door
   <dbl> <chr>                                    <int>  <int> <int> <int>
 1     1 Chair                                        1      0     0     0
 2     2 "Chair- Window/Glass- "                      1      1     0     0
 3     3 " Door- Sofa-"                               1      0     0     1
 4     4 "Chair- Window/Glass Frame- "                1      1     0     0
 5     5 1. Window/Glass Frame                        0      1     0     0
 6     6 Chair- Door- Window-                         1      1     0     1
 7     7 Chair- Sofa - Door- Table-                   1      0     1     1
 8     8 " 4. Table"                                  0      0     1     0
 9     9 Couch (2)                                    1      0     0     0
10    10 Window- Table- Chair- Sofa- Door- Couach     1      1     1     1
11    11 2. Door / Chair                              1      0     0     1 

数据:

df <- tibble(House = c(1,2,3,4,5,6,7,8,9,10,11), Items = c("Chair", "Chair- Window/Glass- "," Door- Sofa-", "Chair- Window/Glass Frame- ", "1. Window/Glass Frame", "Chair- Door- Window-", "Chair- Sofa - Door- Table-", " 4. Table", "Couch (2)", "Window- Table- Chair- Sofa- Door- Couach", "2. Door / Chair"))