r - 如何通过删除 R 中的括号将字符列拆分为两列?

标签 r string dataframe split

我有每个地理区域或理事会的社会关怀支出理事会数据,如下所示:

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 中执行此操作?

最佳答案

这是使用 groupingregular 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/

相关文章:

python - 迭代读取 Pandas DataFrame 的 (tsv) 文件

r 生成带有随机 1 和 0 且有限制的列

c# - 将 bool 表达式字符串转换为 .NET 代码

r - 如何使用带有 knitr 的 ggplot2 删除 _printed_ 输出警告

python - 如何在 Python 字符串中去掉逗号

java - 在字符串末尾添加随机字符?

python - 如何根据多个条件按变量组有效更新数据帧值?

python - Pandas 将数据从索引表添加到数据框

r - 使用精确匹配和模糊匹配在 R 中连接两个大型数据集

r - 避免控制台消息形式封装函数