如何在数据框架的列名中修改空格(删除空格,注入点)? [英] How to fix spaces in column names of a data.frame (remove spaces, inject dots)?
问题描述
导入文件后,我总是尝试从列名中删除空格,使引用更易于列名。
After importing a file, I always try try to remove spaces from the column names to make referral to column names easier.
有没有更好的方法来做然后使用转换然后删除此命令创建的额外列?
Is there a better way to do this other then using transform and then removing the extra column this command creates?
这是我现在使用的:
names(ctm2)
#tranform function does this, but requires some action
ctm2<-transform(ctm2,dymmyvar=1)
#remove dummy column
ctm2$dymmyvar <- NULL
names(ctm2)
推荐答案
为此目的存在更优雅和一般的解决方案:
There exists more elegant and general solution for that purpose:
tidy.name.vector <- make.names(name.vector, unique=TRUE)
make .names()
在字符向量中使用语法有效的名称。语法有效的名称由字母,数字和点或下划线字符组成,并以字母或点后面的数字开头。
make.names()
makes syntactically valid names out of character vectors. A syntactically valid name consists of letters, numbers and the dot or underline characters and starts with a letter or the dot not followed by a number.
另外,标记 unique = TRUE
允许您避免新列中的可能的公开。
Additionally, flag unique=TRUE
allows you to avoid possible dublicates in new column names.
这篇关于如何在数据框架的列名中修改空格(删除空格,注入点)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!