我有每个地理区域或理事会的社会关怀支出理事会数据,如下所示:
Council Expenditure
Cumbria (102) 100
South Tyneside (109) 200
Bexley (718) 150
Nottingham (512) 178
正如您在数据框的 Council
列中看到的那样,您在括号中给出了理事会名称及其各自的代码,即 (102)、(109) 等。
但我想将理事会名称及其各自的代码分成两个不同的列,并删除理事会代码周围的括号,使其看起来更像这样:
Council Council Code Expenditure
Cumbria 102 100
South Tyneside 109 200
Bexley 718 150
Nottingham 178 178
我在 Stackoverflow 上查看了其他类似的帖子来解决这些类型的问题,并使用了字符串操作数组,例如 strsplit()
、gsub()
等,但是徒劳无功。我特别难以理解括号。
您能否建议我如何在 R 中执行此操作?
最佳答案
这是使用 grouping
和 regular expression
完成它的一种方法:
数据:
Council <- read.table(
text = "Council,Expenditure
Cumbria (102),100
South Tyneside (109),200
Bexley (718),150
Nottingham (512),78",
header = T,
sep = ",",
stringsAsFactors = F
)
代码:
Council <- transform(Council,
# Get the Coucil_Code column
Council_Code = as.numeric(gsub("([^\\d]+)(\\d+)(\\))","\\2",
Council,
perl = T)),
# Clean up the Council column
Council = trimws(gsub("([a-zA-z\\s]+)([\\d\\(\\)]+)","\\1",
Council,
perl = T))
)
输出:
Council Expenditure Council_Code
Cumbria 100 102
South Tyneside 200 109
Bexley 150 718
Nottingham 78 512
希望对您有所帮助。
关于r - 如何通过删除 R 中的括号将字符列拆分为两列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40063626/