我有一个包含调查回复的数据框,每行代表一个不同的人。一栏 - "文字" - 是一个开放式的文本问题。我想使用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)
然后我将Text列变成了字符......
df$Text<-as.character(df$Text)
接下来,我按id列分组并嵌套数据帧。
df<-df%>%mutate(id=row_number())%>%group_by(id)%>%unnest_tokens(word,Text)%>%nest(-id)
到目前为止似乎工作正常,但现在我如何使用purrr :: map函数处理嵌套列表列&#34; word&#34;?例如,如果我想使用dplyr :: mutate创建一个新列,每行有字数?
此外,是否有更好的方法来嵌套数据帧,以便只有&#34; Text&#34;列是嵌套列表吗?
答案 0 :(得分:0)
我喜欢使用purrr::map
来numpy.setdiff1d
,但是对于你所说的,我认为你可以坚持使用直接的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_by
和mutate
。
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
您可以通过实施内部联接来进行情绪分析;看看modeling for different groups。