如何在单个数据框列中记录 presence/absence 大字符串的拆分元素

How to record presence/absence of splitted elements of large strings in individual dataframe columns

我有一个数据框,其中包含长而杂乱的家庭设施。我想将字符串分解为独特的设施,为每个独特的设施在数据框中创建一个新列,并在新列中记录字符串中各个设施的 presence/absence。 使用嵌套的 for 循环,我找到了完成任务的方法。但是,我想知道的是如何使用 apply 函数族之一或 dplyr 方法来避免循环来获得相同的结果。

可重现的数据:

df <- data.frame(
  id = 1:4,
  amenities = c('{"Wireless Internet","Wheelchair accessible",Kitchen,Elevator,"Buzzer/wireless intercom",Heating}',
                '{TV,"Cable TV",Internet,"Wireless Internet","Air conditioning",Kitchen,"Smoking allowed","Pets allowed"}',
                '{"Buzzer/wireless intercom",Heating,"Family/kid friendly","Smoke detector",Carbon monoxide}',
                '{Washer,Dryer,Essentials,Shampoo,Hangers,"Laptop friendly workspace"}'))

到目前为止我所做的是:

amenities_clean <- gsub('[{}"]', '', df$amenities) # remove unwanted stuff 
amenities_split <- strsplit(amenities_clean, ",") # split rows into individual amenities
amenities_unique <- unique(unlist(strsplit(amenities_clean, ","))) # get a list of unique amenities 
df[amenities_unique] <- NA # set up the columns for each amenity

为了在新列中记录各个便利设施是否存在于字符串中,我使用了 str_detect 以及嵌套 for 循环:

# record presence/absence of individual amenities in each new column:
library(stringr)
for(i in 1:ncol(df[amenities_unique])){
  for(j in 1:nrow(df)){
    df[amenities_unique][j,i] <- 
      ifelse(str_detect(amenities_split[j], names(df[amenities_unique][i])), 1, 0)
  }
}

虽然这会产生警告,但它们似乎无害,因为结果看起来不错:

df
  id                                                                                                amenities Wireless Internet
1  1        {"Wireless Internet","Wheelchair accessible",Kitchen,Elevator,"Buzzer/wireless intercom",Heating}                 1
2  2 {TV,"Cable TV",Internet,"Wireless Internet","Air conditioning",Kitchen,"Smoking allowed","Pets allowed"}                 1
3  3              {"Buzzer/wireless intercom",Heating,"Family/kid friendly","Smoke detector",Carbon monoxide}                 0
4  4                                    {Washer,Dryer,Essentials,Shampoo,Hangers,"Laptop friendly workspace"}                 0
  Wheelchair accessible Kitchen Elevator Buzzer/wireless intercom Heating TV Cable TV Internet Air conditioning Smoking allowed
1                     1       1        1                        1       1  0        0        1                0               0
2                     0       1        0                        0       0  1        1        1                1               1
3                     0       0        0                        1       1  0        0        0                0               0
4                     0       0        0                        0       0  0        0        0                0               0
  Pets allowed Family/kid friendly Smoke detector Carbon monoxide Washer Dryer Essentials Shampoo Hangers Laptop friendly workspace
1            0                   0              0               0      0     0          0       0       0                         0
2            1                   0              0               0      0     0          0       0       0                         0
3            0                   1              1               1      0     0          0       0       0                         0
4            0                   0              0               0      1     1          1       1       1                         1

鉴于警告和嵌套循环的复杂性,如何使用 apply 函数族中的函数或使用 dplyr 获得相同的结果?

清洁便利设施后,您可以使用 splitstackshape 中的 cSplit_e

df$amenities_clean <- gsub('[{}"]', '', df$amenities) 
splitstackshape::cSplit_e(df, "amenities_clean", type = "character", fill = 0)

要使用我们可以执行的应用函数之一来解决它:

temp <- strsplit(df$amenities_clean, ",")
amenities_unique <- unique(unlist(temp))
cbind(df, t(sapply(temp, function(x) 
                   table(factor(x, levels = amenities_unique)))))

我相信这会提供您需要的输出:

library(tidyverse)

df %>%
  mutate(amenities = str_replace_all(amenities, '["{}]', '')) %>% 
  separate_rows(amenities, sep = ",") %>% 
  pivot_wider(names_from = amenities, values_from = amenities, values_fn = list(amenities = is.character)) %>% 
  mutate_all(replace_na, 0) 

结果是:

# A tibble: 4 x 22
     id `Wireless Inter~ `Wheelchair acc~ Kitchen Elevator `Buzzer/wireles~ Heating    TV `Cable TV` Internet `Air conditioni~ `Smoking allowe~
  <dbl>            <dbl>            <dbl>   <dbl>    <dbl>            <dbl>   <dbl> <dbl>      <dbl>    <dbl>            <dbl>            <dbl>
1     1                1                1       1        1                1       1     0          0        0                0                0
2     2                1                0       1        0                0       0     1          1        1                1                1
3     3                0                0       0        0                1       1     0          0        0                0                0
4     4                0                0       0        0                0       0     0          0        0                0                0
# ... with 10 more variables: `Pets allowed` <dbl>, `Family/kid friendly` <dbl>, `Smoke detector` <dbl>, `Carbon monoxide` <dbl>, Washer <dbl>,
#   Dryer <dbl>, Essentials <dbl>, Shampoo <dbl>, Hangers <dbl>, `Laptop friendly workspace` <dbl>