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

标签 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),得到一个唯一列表(unique),然后用我想要的标准(grepl)替换所有变体,最后根据类别。

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

这就是我想要得到的: ( window 和玻璃是相同的,椅子/沙发/沙发是相同的,等等 - 所以我只需要创建更大的类别,而不是有几列本质上相同的东西)

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 |
+-------+-------+--------+-------+------+

最佳答案

您可以在map_df(或sapply)中使用str_detect(或grepl)来生成以下数据帧:逻辑,将它们强制为整数 0/1,然后将其绑定(bind)到原始数​​据帧。这种方法绕过了 split /清洁等的麻烦。数据。它只需要您首先为正则表达式创建模式组,即 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"))

关于r - 将不均匀的字符串拆分为 R 中的排序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56792985/

相关文章:

algorithm - 如何从用户输入生成唯一的字符串?

r - 如何确定哪些程序包依赖于R中的给定程序包?

r - 如何将多个工作簿合并为一个显示为选项卡/工作表的 Excel 文件?

r - 'x' 必须是数值向量 : Error from data. 数字框

c++ - 是否可以使用 std::sort 按词典顺序排序?

unique - lsFusion:属性 NULL 的唯一性限制

r - 在 R 中使用双引号 ("")

Javascript - Opera 11.60 和 IE 8 上的排序功能问题

r - 将 r 中较小的序列映射(对齐)到较大的序列

Laravel 5.4 表单请求唯一验证: how to access id?