1

我有一个数据框,其中包含调查回复,每行代表一个不同的人。一栏——“文本”——是一个开放式文本问题。我想使用 Tidytext::unnest_tokens 以便按每一行进行文本分析,包括情感分数、字数等。

这是此示例的简单数据框:

Satisfaction<-c ("Satisfied","Satisfied","Dissatisfied","Satisfied","Dissatisfied")
Text<-c("I'm very satisfied with the services", "Your service providers are always late which causes me a lot of frustration", "You should improve your staff training, service providers have bad customer service","Everything is great!","Service is bad")
Gender<-c("M","M","F","M","F")
df<-data.frame(Satisfaction,Text,Gender)

然后我把文本列变成了字符......

df$Text<-as.character(df$Text)

接下来我按 id 列分组并嵌套数据框。

df<-df%>%mutate(id=row_number())%>%group_by(id)%>%unnest_tokens(word,Text)%>%nest(-id)

走到这一步似乎还不错,但现在我如何使用 purrr::map 函数处理嵌套列表列“word”?例如,如果我想使用 dplyr::mutate 创建一个新列,并为每行提供字数?

此外,是否有更好的方法来嵌套数据框,以便只有“文本”列是嵌套列表?

4

1 回答 1

0

我喜欢为不同的群体purrr::map建模,但对于你所说的,我认为你可以坚持直接使用 dplyr。

您可以像这样设置数据框:

library(dplyr)
library(tidytext)

Satisfaction <- c("Satisfied",
                  "Satisfied",
                  "Dissatisfied",
                  "Satisfied",
                  "Dissatisfied")

Text <- c("I'm very satisfied with the services",
          "Your service providers are always late which causes me a lot of frustration", 
          "You should improve your staff training, service providers have bad customer service",
          "Everything is great!",
          "Service is bad")

Gender <- c("M","M","F","M","F")

df <- data_frame(Satisfaction, Text, Gender)

tidy_df <- df %>% 
    mutate(id = row_number()) %>% 
    unnest_tokens(word, Text)

然后,例如要查找每行的单词数,您可以使用group_bymutate

tidy_df %>%
    group_by(id) %>%
    mutate(num_words = n()) %>%
    ungroup
#> # A tibble: 37 × 5
#>    Satisfaction Gender    id      word num_words
#>           <chr>  <chr> <int>     <chr>     <int>
#> 1     Satisfied      M     1       i'm         6
#> 2     Satisfied      M     1      very         6
#> 3     Satisfied      M     1 satisfied         6
#> 4     Satisfied      M     1      with         6
#> 5     Satisfied      M     1       the         6
#> 6     Satisfied      M     1  services         6
#> 7     Satisfied      M     2      your        13
#> 8     Satisfied      M     2   service        13
#> 9     Satisfied      M     2 providers        13
#> 10    Satisfied      M     2       are        13
#> # ... with 27 more rows

您可以通过实现内部连接来进行情感分析;在这里查看一些示例

于 2017-04-07T19:43:13.957 回答