[英]splitting values in a dataframe R
我在 R 中有這個數據框。df 的名稱:POST
Order_id Postcalcode
1 5253HP
2 4261FF
44 5111QW
我想從郵政編碼中拆分數字和字母,因為我只需要預期的數字 output 是
Order_id Postalcode
1 5253
2 4261
44 5111
library(magrittr)
library(dplyr)
library(stringr)
df %>% mutate(Postcalcode = str_extract(Postcalcode,"\\d+"))
Order_id Postcalcode
1 1 5253
2 2 4261
3 44 5111
此外, tidyr::separate
可能值得一看。 在下面的示例中,拆分發生在定義的 position。
dta <- readr::read_table(file = "
Order_id Postcalcode
1 5253HP
2 4261FF
44 5111QW")
tidyr::separate(
data = dta,
col = "Postcalcode",
into = c("first_part", "second_part"),
sep = 4,
remove = FALSE
)
結果:
# A tibble: 3 × 4
Order_id Postcalcode first_part second_part
<dbl> <chr> <chr> <chr>
1 1 5253HP 5253 HP
2 2 4261FF 4261 FF
3 44 5111QW 5111 QW
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.