dplyr:非标准列名(空格、标点符号、以数字开头) [英] dplyr: nonstandard column names (white space, punctuation, starts with numbers)
问题描述
df <- structure(list(`a a` = 1:3, `a b` = 2:4), .Names = c("a a", "a b"
), row.names = c(NA, -3L), class = "data.frame")
数据看起来像
a a a b
1 1 2
2 2 3
3 3 4
以下调用选择
select(df, 'a a')
给予
Error in abs(ind[ind < 0]) :
non-numeric argument to mathematical function
如何使用 select
选择a a"和/或将其重命名为不带空格的名称?我知道以下方法:
How can I select "a a" and/or rename it to something without space using select
? I know the following approaches:
names(df)[1] <- "a"
select(df, a=1)
select(df,ends_with("a"))
但是如果我正在处理一个大型数据集,我如何在不知道索引号或类似列名的情况下获得完全匹配?
but if I am working on a large data set, how can I get an exact match without knowing the index numer or similar column names?
推荐答案
您可以使用反引号`
select
变量.
You may select
the variable by using backticks `
.
select(df, `a a`)
# a a
# 1 1
# 2 2
# 3 3
但是,如果您的主要目标是重命名列,则可以使用 plyr
包中的 rename
,其中可以同时使用 ""
和 ``
.
However, if your main objective is to rename the column, you may use rename
in plyr
package, in which you can use both ""
and ``
.
rename(df, replace = c("a a" = "a"))
rename(df, replace = c(`a a` = "a"))
或者在 base
R:
names(df)[names(df) == "a a"] <- "a"
有关各种引号使用的更详尽说明,请参阅?Quotes
.名称和标识符"部分在此处特别相关:
For a more thorough description on the use of various quotes, see ?Quotes
. The 'Names and Identifiers' section is especially relevant here:
可以使用其他 [语法无效] 名称,前提是它们被引用.首选引号是反引号".
other [syntactically invalid] names can be used provided they are quoted. The preferred quote is the backtick".
另见关于有效名称的 ?make.names
.
See also ?make.names
about valid names.
另请参阅这篇文章关于在<代码>dplyr
这篇关于dplyr:非标准列名(空格、标点符号、以数字开头)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!