R - Yelp数据业务类别列每个业务有多个类别。想要分成值为1和0的类别特定列

问题描述 投票:1回答:2

提前感谢任何想要尝试和帮助的人。

我正在使用Yelp数据集,我想回答的问题是“哪些类别与X类别的高星(例如条形图)正相关”

我遇到的问题是,对于每个业务,类别被集中在一个列和每个businesss_id行。因此,我需要一种方法来分离每个类别,将它们转换为列,然后检查原始类别列是否包含为其创建列的类别。

我目前的思路是使用group_by和business_id然后对列进行unfst_tokens,然后将model.matrix()列放入我想要的分割中,然后将其加入到我正在使用的df中。但我不能让model.matrix传递并保持business_id连接到每一行。

# an example of what I am using #
df <- 
  data_frame(business_id = c("bus_1",
                             "bus_2", 
                             "bus_3"),
             categories=c("Pizza, Burgers, Caterers",
                          "Pizza, Restaurants, Bars",
                          "American, Barbeque, Restaurants"))

# what I want it to look like #
desired_df <- 
  data_frame(business_id = c("bus_1",
                             "bus_2",
                             "bus_3"),
             categories=c("Pizza, Burgers, Caterers",
                          "Pizza, Restaurants, Bars",
                          "American, Barbeque, Restaurants"),
             Pizza = c(1, 1, 0),
             Burgers = c(1, 0, 0),
             Caterers = c(1, 0, 0),
             Restaurants = c(0, 1, 1),
             Bars = c(0, 1, 0),
             American = c(0, 0, 1),
             Barbeque = c(0, 0, 1))

# where I am stuck #
df %>%
  select(business_id, categories) %>% 
  group_by(business_id) %>% 
  unnest_tokens(categories, categories, token = 'regex', pattern=", ") %>%
  model.matrix(business_id ~ categories, data = .) %>% 
  as_data_frame

编辑:在这篇文章和下面的答案之后,我遇到了使用spread()的重复标识符错误。这让我进入了这个帖子https://github.com/tidyverse/tidyr/issues/426我的问题的答案发布了,我在下面重新编写了它。

#用较小的data.frame复制错误#

library(tidyverse)
 df <- structure(list(age = c("21", "17", "32", "29", "15"), 
                        gender = structure(c(2L, 1L, 1L, 2L, 2L), .Label = c("Female", "Male"), class = "factor")), 
                   row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"), .Names = c("age", "gender"))
 df
#> # A tibble: 5 x 2
#>   age   gender
#>   <chr> <fct> 
#> 1 21    Male  
#> 2 17    Female
#> 3 32    Female
#> 4 29    Male  
#> 5 15    Male  

df %>% 
  spread(key=gender, value=age)
#> Error: Duplicate identifiers for rows (2, 3), (1, 4, 5)

#修复问题#

df %>% 
  group_by_at(vars(-age)) %>%  # group by everything other than the value column. 
  mutate(row_id=1:n()) %>% ungroup() %>%  # build group index
  spread(key=gender, value=age) %>%    # spread
  select(-row_id)  # drop the index

#> # A tibble: 3 x 2
#>   Female Male 
#>   <chr>  <chr>
#> 1 17     21   
#> 2 32     29   
#> 3 NA     15   
r dataframe yelp
2个回答
0
投票

在您使用tidytext::unnest_tokens()的基础上进行构建,您也可以使用此替代解决方案

library(dplyr)
library(tidyr)
library(tidytext)

df %>%
  select(business_id, categories) %>% 
  group_by(business_id) %>% 
  unnest_tokens(categories, categories, token = 'regex', pattern=", ") %>% 
  mutate(value = 1) %>% 
  spread(categories, value, fill = 0)
# business_id american barbeque  bars burgers caterers pizza restaurants
# <chr>          <dbl>    <dbl> <dbl>   <dbl>    <dbl> <dbl>       <dbl>
# bus_1              0        0     0       1        1     1           0
# bus_2              0        0     1       0        0     1           1
# bus_3              1        1     0       0        0     0           1

0
投票

这是一个简单的整数解决方案:

library(tidyverse)

df %>% 
  mutate(
    ind = 1,
    tmp = strsplit(categories, ", ")
  ) %>% 
  unnest(tmp) %>% 
  spread(tmp, ind, fill = 0)
## A tibble: 3 x 9
#  business_id categories                      American Barbeque  Bars Burgers Caterers Pizza Restaurants
#  <chr>       <chr>                              <dbl>    <dbl> <dbl>   <dbl>    <dbl> <dbl>       <dbl>
#1 bus_1       Pizza, Burgers, Caterers               0        0     0       1        1     1           0
#2 bus_2       Pizza, Restaurants, Bars               0        0     1       0        0     1           1
#3 bus_3       American, Barbeque, Restaurants        1        1     0       0        0     0           1
© www.soinside.com 2019 - 2024. All rights reserved.