r - 如何在 R 中将一个字符列拆分为多个列

我有一个数据框x:

dput(x)
structure(list(District = structure(c(6L, 6L, 6L, 6L, 6L, 6L), .Label = c("District - Central (06)", 
"District - East (04)", "District - New Delhi (05)", "District - North (02)", 
"District - North East (03)", "District - North West (01)", "District - South (09)", 
"District - South West (08)", "District - West (07)"), class = "factor"), 
    Age = structure(c(103L, 1L, 2L, 14L, 25L, 36L), .Label = c("0", 
    "1", "10", "100+", "11", "12", "13", "14", "15", "16", "17", 
    "18", "19", "2", "20", "21", "22", "23", "24", "25", "26", 
    "27", "28", "29", "3", "30", "31", "32", "33", "34", "35", 
    "36", "37", "38", "39", "4", "40", "41", "42", "43", "44", 
    "45", "46", "47", "48", "49", "5", "50", "51", "52", "53", 
    "54", "55", "56", "57", "58", "59", "6", "60", "61", "62", 
    "63", "64", "65", "66", "67", "68", "69", "7", "70", "71", 
    "72", "73", "74", "75", "76", "77", "78", "79", "8", "80", 
    "81", "82", "83", "84", "85", "86", "87", "88", "89", "9", 
    "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", 
    "Age not stated", "All ages"), class = "factor"), Total = c(3656539L, 
    56131L, 58644L, 63835L, 63859L, 64945L), Rural = c(213950L, 
    3589L, 3757L, 4200L, 4102L, 4223L), Urban = c(3442589L, 52542L, 
    54887L, 59635L, 59757L, 60722L)), .Names = c("District", 
"Age", "Total", "Rural", "Urban"), row.names = c(NA, 6L), class = "data.frame")

我想拆分 District 列以将地区名称提取到新列 Name 中。例如。 “District - North West (01)”应该拆分为“North West”。 我试过 str_split_fixed 并得到:

x
                    District      Age   Total  Rural   Urban 1    name
1 District - North West (01) All ages 3656539 213950 3442589      North West (01)
2 District - North West (01)        0   56131   3589   52542      North West (01)
3 District - North West (01)        1   58644   3757   54887      North West (01)
4 District - North West (01)        2   63835   4200   59635      North West (01)
5 District - North West (01)        3   63859   4102   59757      North West (01)
6 District - North West (01)        4   64945   4223   60722      North West (01)

我尝试再次使用相同的函数来拆分 name 列以将地区名称与代码分开,但它给了我以下错误:

Error in stri_split_regex(string, pattern, n = n, simplify = TRUE, opts_regex = attr(pattern, : Incorrectly nested parentheses in regexp pattern. (U_REGEX_MISMATCHED_PAREN)

有没有办法在单个函数中根据模式将字符列拆分为多个列?

最佳答案

你可以使用

library(stringr)

data.frame(str_split_fixed(df$District, "", 3))

    X1      X2      X3
1 District   -    North West (01)
2 District   -    North West (01)
3 District   -    North West (01)
4 District   -    North West (01)
5 District   -    North West (01)
6 District   -    North West (01)

您可以使用 gsub 删除您在此处拥有的多余内容,

gsub("[[:digit:]]","",df$X3)
gsub("[[:punct:]]","",df$X3)

等等

https://stackoverflow.com/questions/38458388/

相关文章:

r - R中二维核密度估计的混淆

c# - 使用 Xamarin android 从 Asset 加载文件

java - 在不同计算机之间同步 IntelliJ 项目

sql - 数据库中每个表的最大值(ID)

php - 如何在 laravel 中返​​回一个响应对象 json

php - PHP 中的 fatal error 是什么意思?

ansible - 具有 SSH key 身份验证的用户如何在 Ansible 中拥有 sudo 权

scala - 为什么 For Comprehension 生成器会抑制 Option 类型?

php - 如何取消设置特定用户的 session ?

cakephp - 如何从cakephp 3中的url访问参数