替换列名gsub中的字符 [英] Replace characters in column names gsub

查看:98
本文介绍了替换列名gsub中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读一堆CSV,它们的标题中包含"sales-数千"之类的内容,并以"sales ...数千"的形式出现在R中.我想使用正则表达式(或其他简单方法)来清理它们.

I am reading in a bunch of CSVs that have stuff like "sales - thousands" in the title and come into R as "sales...thousands". I'd like to use a regular expression (or other simple method) to clean these up.

我不知道为什么这不起作用:

I can't figure out why this doesn't work:

#mock data
  a <- data.frame(this.is.fine = letters[1:5],
                  this...one...isnt = LETTERS[1:5])

#column names
  colnames(a)
  # [1] "this.is.fine"  "this...one...isnt"

#function to remove multiple spaces
  colClean <- function(x){
    colnames(x) <- gsub("\\.\\.+", ".", colnames(x))
  }

#run function
  colClean(a)

#names go unaffected
  colnames(a)
  # [1] "this.is.fine"  "this...one...isnt"

但是此代码可以:

#direct change to names
  colnames(a) <- gsub("\\.\\.+", ".", colnames(a))

#new names
  colnames(a)
  # [1] "this.is.fine"  "this.one.isnt"

请注意,我可以在单词之间留一个句点.

Note that I'm fine leaving one period between words when that occurs.

谢谢.

推荐答案

Rich Scriven 有答案:

定义

colClean <- function(x){ colnames(x) <- gsub("\\.\\.+", ".", colnames(x)); x } 

然后做

a <- colClean(a) 

更新

这篇关于替换列名gsub中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆