如何在r中的第n个字符之后拆分字符串

Sha*_*ani 2 string split r data-management

我正在处理以下数据:

District <- c("AR01", "AZ03", "AZ05", "AZ08", "CA01", "CA05", "CA11", "CA16", "CA18", "CA21")
Run Code Online (Sandbox Code Playgroud)

我想在第二个字符之后拆分字符串并将它们分成两列。

使数据看起来像这样:

state  district
AR        01
AZ        03
AZ        05
AZ        08
CA        01
CA        05
CA        11
CA        16
CA        18
CA        21
Run Code Online (Sandbox Code Playgroud)

有没有简单的代码来完成这项工作?非常感谢你的帮助

Mik*_*ike 6

substr如果您总是想按第二个字符拆分,则可以使用。

District <- c("AR01", "AZ03", "AZ05", "AZ08", "CA01", "CA05", "CA11", "CA16", "CA18", "CA21")
#split district  starting at the first and ending at the second
state <- substr(District,1,2)
#split district starting at the 3rd and ending at the 4th
district <- substr(District,3,4)
#put in data frame if needed.
st_dt <- data.frame(state = state, district = district, stringsAsFactors = FALSE)
Run Code Online (Sandbox Code Playgroud)


Ony*_*mbu 5

你可以strcapture从基础 R使用:

 strcapture("(\\w{2})(\\w{2})",District,
                    data.frame(state = character(),District = character()))
   state District
1     AR       01
2     AZ       03
3     AZ       05
4     AZ       08
5     CA       01
6     CA       05
7     CA       11
8     CA       16
9     CA       18
10    CA       21
Run Code Online (Sandbox Code Playgroud)

where\\w{2}意味着两个词