在r中,我目前正在使用会话数据集。目前的数据如下:
Mike, "Hello how are you"
Sally, "Good you"
我计划最终创建这个数据的文字云,并且需要它看起来像这样:
Mike, Hello
Mike, how
Mike, are
Mike, you
Sally, good
Sally, you
答案 0 :(得分:2)
使用reshape2::melt
可能是这样的吗?
# Sample data
df <- read.csv(text =
'Mike, "Hello how are you"
Sally, "Good you"', header = F)
# Split on words
lst <- strsplit(trimws(as.character(df[, 2])), "\\s");
names(lst) <- trimws(df[, 1]);
# Reshape into long dataframe
library(reshape2);
df.long <- (melt(lst))[2:1];
# L1 value
#1 Mike Hello
#2 Mike how
#3 Mike are
#4 Mike you
#5 Sally Good
#6 Sally you
说明:在空格trimws
的第二列中拆分尾随/前导空格(\\s
)条目并存储在list
中。从第一列中获取list
条目名称,然后使用data.frame
重塑为长reshape2::melt
。
我把它变成逗号分隔的data.frame
直到你......
答案 1 :(得分:0)
使用标记器,例如通过tidytext::unnest_tokens
:
library(tidyverse)
library(tidytext)
dialogue <- read_csv(
'Mike, "Hello how are you"
Sally, "Good you"',
col_names = c('speaker', 'sentence')
)
dialogue %>% unnest_tokens(word, sentence)
#> # A tibble: 6 x 2
#> speaker word
#> <chr> <chr>
#> 1 Mike hello
#> 2 Mike how
#> 3 Mike are
#> 4 Mike you
#> 5 Sally good
#> 6 Sally you