如何在data.frame的列名中固定空格(删除空格,注入点)?

时间:2021-07-31 07:58:47

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?

是否有更好的方法,然后使用转换,然后删除这个命令创建的额外列?

This is what I use now:

这就是我现在使用的:

names(ctm2)
#tranform function does this, but requires some action
ctm2<-transform(ctm2,dymmyvar=1)
#remove dummy column
ctm2$dymmyvar <- NULL
names(ctm2)

6 个解决方案

#1


49  

There exists more elegant and general solution for that purpose:

为此目的存在着更为优雅和普遍的解决办法:

tidy.name.vector <- make.names(name.vector, unique=TRUE)

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.

名称()将从字符向量中提取出语法有效的名称。语法上有效的名称由字母、数字和点或下划线组成,以字母或点开头,而不是以数字结尾。

Additionally, flag unique=TRUE allows you to avoid possible dublicates in new column names.

此外,flag unique=TRUE允许您避免在新的列名中使用可能的dublicates。

As code to implement

作为代码来实现

d<-read_delim(urltxt,delim='\t',)
names(d)<-make.names(names(d),unique = TRUE)

#2


15  

To replace only the first space in each column you could also do:

为了只替换每个列中的第一个空格,您还可以这样做:

names(ctm2) <- sub(" ", ".", names(ctm2))

or to replace all spaces (which seems like it would be a little more useful):

或者替换所有的空格(这似乎更有用一些):

names(ctm2) <- gsub(" ", "_", names(ctm2))

or, as mentioned in the first answer (though not in a way that would fix all spaces):

或者,正如第一个答案中提到的(虽然不是以一种可以修复所有空格的方式):

spaceless <- function(x) {colnames(x) <- gsub(" ", "_", colnames(x));x}
newDF <- spaceless(ctm2)

where x is the name of your data.frame. I prefer to use "_" to avoid issues with "." as part of an ID.

其中x是您的data.frame的名称。我更喜欢使用“_”来避免“.”作为ID的一部分。

The point is that gsub doesn't stop at the first instance of a pattern match.

重点是,gsub不会在模式匹配的第一个实例上停止。

#3


1  

Just assign to names(ctm2):

只是分配名称(ctm2):

  names(ctm2) <- c("itsy", "bitsy", "eeny", "meeny")

or in data-driven way:

或以数据驱动方式:

  names(ctm2) <- paste("myColumn", 1:ncol(ctm2), sep="")

Another possibility is to edit your source file...

另一种可能是编辑你的源文件…

#4


1  

There is an easy way to remove spaces in column names in data.table. You will have to convert your data frame to data table.

有一种简单的方法可以删除data.table中的列名中的空格。您必须将数据帧转换为数据表。

setnames(x=DT, old=names(DT), new=gsub(" ","",names(DT)))

Country Code will be converted to CountryCode

国家代码将被转换为国家代码

#5


1  

Assign the names like this. This works best. It replaces all white spaces in the name with underscore.

像这样分配名字。这是最好的。它用下划线替换名称中的所有空格。

names(ctm2)<-gsub("\\s","_",names(ctm2))

名(ctm2)< -gsub(“\ \”,“_”,名字(ctm2))

#6


0  

Alternatively, you may be able to achieve the same results with the stingr package.

或者,您可以使用stingr包实现相同的结果。

names(ctm2)<-names(ctm2)%>% stringr::str_replace_all("\\s","_")

名(ctm2) <名称(ctm2)%> % stringr::str_replace_all(“\ \”,“_”)

#1


49  

There exists more elegant and general solution for that purpose:

为此目的存在着更为优雅和普遍的解决办法:

tidy.name.vector <- make.names(name.vector, unique=TRUE)

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.

名称()将从字符向量中提取出语法有效的名称。语法上有效的名称由字母、数字和点或下划线组成,以字母或点开头,而不是以数字结尾。

Additionally, flag unique=TRUE allows you to avoid possible dublicates in new column names.

此外,flag unique=TRUE允许您避免在新的列名中使用可能的dublicates。

As code to implement

作为代码来实现

d<-read_delim(urltxt,delim='\t',)
names(d)<-make.names(names(d),unique = TRUE)

#2


15  

To replace only the first space in each column you could also do:

为了只替换每个列中的第一个空格,您还可以这样做:

names(ctm2) <- sub(" ", ".", names(ctm2))

or to replace all spaces (which seems like it would be a little more useful):

或者替换所有的空格(这似乎更有用一些):

names(ctm2) <- gsub(" ", "_", names(ctm2))

or, as mentioned in the first answer (though not in a way that would fix all spaces):

或者,正如第一个答案中提到的(虽然不是以一种可以修复所有空格的方式):

spaceless <- function(x) {colnames(x) <- gsub(" ", "_", colnames(x));x}
newDF <- spaceless(ctm2)

where x is the name of your data.frame. I prefer to use "_" to avoid issues with "." as part of an ID.

其中x是您的data.frame的名称。我更喜欢使用“_”来避免“.”作为ID的一部分。

The point is that gsub doesn't stop at the first instance of a pattern match.

重点是,gsub不会在模式匹配的第一个实例上停止。

#3


1  

Just assign to names(ctm2):

只是分配名称(ctm2):

  names(ctm2) <- c("itsy", "bitsy", "eeny", "meeny")

or in data-driven way:

或以数据驱动方式:

  names(ctm2) <- paste("myColumn", 1:ncol(ctm2), sep="")

Another possibility is to edit your source file...

另一种可能是编辑你的源文件…

#4


1  

There is an easy way to remove spaces in column names in data.table. You will have to convert your data frame to data table.

有一种简单的方法可以删除data.table中的列名中的空格。您必须将数据帧转换为数据表。

setnames(x=DT, old=names(DT), new=gsub(" ","",names(DT)))

Country Code will be converted to CountryCode

国家代码将被转换为国家代码

#5


1  

Assign the names like this. This works best. It replaces all white spaces in the name with underscore.

像这样分配名字。这是最好的。它用下划线替换名称中的所有空格。

names(ctm2)<-gsub("\\s","_",names(ctm2))

名(ctm2)< -gsub(“\ \”,“_”,名字(ctm2))

#6


0  

Alternatively, you may be able to achieve the same results with the stingr package.

或者,您可以使用stingr包实现相同的结果。

names(ctm2)<-names(ctm2)%>% stringr::str_replace_all("\\s","_")

名(ctm2) <名称(ctm2)%> % stringr::str_replace_all(“\ \”,“_”)