简体   繁体   English

R更改包含加号或方括号的列名称

[英]R changing column names containing plus or brackets

I am saving my column names of a data.frame into a variable in R . 我将data.frame列名data.frameR的变量中。 Some of my columns names contain a plus sign + . 我的某些列名称包含加号+ R changes the + into a . R+更改为. when saving it into a variable. 将其保存到变量中时。 I would like to retain the + so that i can pick these columns again automatically when I need them. 我想保留+以便在需要时可以自动再次选择这些列。

Here is the command I am using to save the column names into a variable: 这是我用来将列名保存到变量中的命令:

for (u in 1:50) {
    k <- colnames[u]
    f <- append(f,k)
}
## f is defined previously in my program

Here is the command I am using to get the names I need again: 这是我用来重新获取所需名称的命令:

file2 <- file1[,f]

Example: column1+ is named column1. 示例: column1+被命名为column1. in the variable f 在变量f

Note: This happened to the brackets () as well as slashes / 注意:括号()和斜杠/

Any Ideas how I can get around this problem? 有什么想法可以解决这个问题吗?

Just so the question remains answered. 只是这个问题仍然得到回答。 Set the option check.names to FALSE while reading the data.frame using read.table as: 设置选项check.namesFALSE在阅读data.frame使用read.table为:

read.table(file, check.names = FALSE)

Note: As @Roland says under the comments, it is better to keep the column names clean rather than using this parameter. 注意:正如@Roland在注释下所说,最好使列名保持整洁而不是使用此参数。 You may also run into situations where certain functions automatically convert the names back. 您可能还会遇到某些功能会自动将名称转换回原来的情况。 For example, 例如,

df <- data.frame('x+y' = 1:4, 'a+b' = 5:8, check.names = FALSE)
> df
#   x+y a+b
# 1   1   5
# 2   2   6
# 3   3   7
# 4   4   8

# Now adding a 3rd column, using `transform`
transform(df, c=9:12)
#   x.y a.b  c  # note that it reverts back
# 1   1   5  9
# 2   2   6 10
# 3   3   7 11
# 4   4   8 12

transform(df, c=9:12, check.names = FALSE)
#   x+y a+b
# 1   1   5
# 2   2   6
# 3   3   7
# 4   4   8

You'll have to know ALL functions that has check.names=FALSE and remember to use them correctly. 您必须知道所有具有check.names=FALSE函数,并记住正确使用它们。 This is at least one problem I could think of. 这是我至少想到的一个问题。 Its rather better to have the columns without conflict. 使列没有冲突会更好。

Including operators such as + in column names can also interfer with the formula model interface: 在列名中包括+运算符也会干扰公式模型接口:

dat <- data.frame('a+x'=c(1,2,3,4),b=c(2,4,6,8),check.names=FALSE)
lm(dat$b~dat$a+x)
Error in eval(expr, envir, enclos) : object 'x' not found

You would need to use lm(dat$b~dat[,'a+x']) . 您将需要使用lm(dat$b~dat[,'a+x'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM