[英]How to join tokenized words back together in a column in R dataframe
我有一個數據框,其中包含以前標記的單詞,如下所示。 復制代碼:
df <- data.frame (id = c("1", "2","3"),
text = c("['I', 'like', 'apple']", "['we', 'go', 'swimming']", "['ask', 'questions']")
)
:
id text
1 ["I", "like", "apple"]
2 ["we", "go", "swimming"]
3 ["ask", "questions"]
原始數據幀是在預處理(包括標記化)原始文本數據后在 Python 中獲得的。
我想將這些標記合並回一個句子中,如下所示
id text
1 I like apple
2 we go swimming
3 ask questions
我嘗試使用 paste() 函數df$text_new<-paste(df$text, sep = " ")
,但它沒有工作,仍然返回相同的結果。
您可以將它們與 tidyr separate()
然后unite()
。 您必須為最長句子中的每個單詞提供一個足夠長的字符向量into =
-- 我用letters
得到 26 -- 然后引用第一個和最后一個 ( a:z
)。
library(tidyr)
df <- data.frame (id = c("1", "2","3"),
text = c("['I', 'like', 'apple']", "['we', 'go', 'swimming']", "['ask', 'questions']"))
df %>%
separate(text, into = letters, fill = "right") %>%
unite(text, a:z, sep = " ", na.rm = TRUE)
#> id text
#> 1 1 I like apple
#> 2 2 we go swimming
#> 3 3 ask questions
由reprex 包於 2022-05-26 創建 (v2.0.1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.