簡體   English   中英

R:如何根據符號拆分特定的列?

[英]R: How to split a specific column based on symbol?

我正在嘗試在文本挖掘過程中進行POS標記。

這是我的POS標記結果格式。

  Word & POS Tag
1 cmp/NN conditioner/NN
2 contains/VBZ the/DT grinding/VBG
3 diamond/NN

但它與POS標簽混合在一起。 我更喜歡這樣的格式:

  Word                     POS Tag
1 cmp conditioner          NN-NN
2 contains the grinding    VBZ-DT-VBG
3 diamond                  NN

反正有R中的單詞和POS標簽嗎?

用空字符串分別替換/之前和之后的部件。 不使用任何軟件包。

cbind(gsub("/\\w+", "", L), gsub(" ", "-", gsub("\\w+/", "", L)))

贈送:

     [,1]                    [,2]        
[1,] "cmp conditioner"       "NN-NN"     
[2,] "contains the grinding" "VBZ-DT-VBG"
[3,] "diamond"               "NN"  

注意:以可重復形式輸入的假定為:

L <- c("cmp/NN conditioner/NN", "contains/VBZ the/DT grinding/VBG", "diamond/NN")

在使用readLines讀取數據集后,我們可以使用str_extract提取子字符串

v1 <- sapply(str_extract_all(lines[-1], "\\w+(?=[/])"), paste, collapse=" ")
v2 <- sapply(str_extract_all(lines[-1], "(?<=[/])\\w+"), paste, collapse="-")
nm1 <- trimws(scan(text=lines[1], what = "", sep="&", quiet =TRUE))
d1 <- setNames(data.frame(v1, v2, stringsAsFactors= FALSE), nm1)
d1
#                   Word    POS Tag
#1       cmp conditioner      NN-NN
#2 contains the grinding VBZ-DT-VBG
#3               diamond         NN

注意:使用的tidyverse軟件包緊湊且易於使用


或另一種選擇是strsplit

 do.call(rbind, lapply(strsplit(lines[-1], "[/ ]"), function(x) {
      x1 <- x[-1]; c(paste(x1[c(TRUE, FALSE)], collapse=" "), 
      paste(x1[c(FALSE, TRUE)], collapse="-"))}))
 #       [,1]                    [,2]        
 #[1,] "cmp conditioner"       "NN-NN"     
 #[2,] "contains the grinding" "VBZ-DT-VBG"
 #[3,] "diamond"               "NN"     

注意:絕對不使用任何軟件包-100%確認

數據

lines <- readLines("file.txt")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM