簡體   English   中英

將字符串拆分為R中不同長度的子字符串

[英]Splitting string into substrings of different lengths in R

我讀過類似的主題,但我的子串有不同的長度(每個9,3,5個字符),因此沒有找到任何答案。

我需要將17個字符長的字符串分成三個子字符串,其中第一個長度為9,下一個長度為3,最后一個長度為5個字符。

例:

 N12345671004UN005
 N34567892902UN002 

我想將字符串拆分為三列:

第一個col 9 char.length

"N12345671"      
"N34567892"

第二個col 3 char.length

"004"          
"902"

第三列5字符長

"UN005"  
"UN002"

您可以嘗試read.fwf並指定widths

ff <- tempfile()
cat(file=ff, instr, sep='\n')
read.fwf(ff, widths=c(9,3,5), colClasses=rep('character', 3))
#        V1  V2    V3
#1 N12345671 004 UN005
#2 N34567892 902 UN002

或者使用tidyr/dplyr

library(dplyr)
library(tidyr)
as.data.frame(instr) %>%
       extract(instr, into=paste0('V', 1:3), '(.{9})(.{3})(.{5})')
#         V1  V2    V3
#1 N12345671 004 UN005
#2 N34567892 902 UN002

或者subread.table的組合

read.table(text=sub('(.{9})(.{3})(.{5})', '\\1 \\2 \\3', instr),
              colClasses=rep('character', 3))
#         V1  V2    V3
#1 N12345671 004 UN005 
#2 N34567892 902 UN002

數據

instr = c("N12345671004UN005", "N34567892902UN002")
instr = c("N12345671004UN005", "N34567892902UN002")
out1 = substr(instr, 1, 9)
out2 = substr(instr, 10, 12)
out3 = substr(instr, 13, 17)

暫無
暫無

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

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