dplyr:选择包含空格的列名 [英] dplyr: select column names containing white space
问题描述
df< - structure(list(`aa` = 1:3,`ab` = 2:4),.Names = c(aa,ab
),row.names = c(NA,-3L),class =data.frame)
,数据看起来像
aaab
1 1 2
2 2 3
3 3 4
以下调用选择
pre>
select(df,'a a')
给出
abs(ind [ind <0])中的错误:
数学函数的非数值参数
如何使用选择
?我知道以下方法:
-
names(df)[1]< - a
-
select(df,a = 1)
-
select(df,ends_with(a))
但如果我正在一个大的数据集,如何获得完全匹配,而不知道索引数字或类似的列名称?
code>使用反引号选择该变量。
select(df,`aa`)
#aa
#1 1
# 2 2
#3 3
但是,如果您的主要目标是重命名列,您可以在 plyr
包中使用重命名
,您可以在其中使用 code>和
``
。
rename(df, replace = c(aa=a))
重命名(df,replace = c(`aa` =a))
或 base
R:
code> name(df)[names(df)==aa]< - a
有关使用各种引号的更全面的描述,请参阅?Quoutes
。 名称和标识符部分在这里特别相关:其他[语法无效]名称可以被使用,只要它们被引用,首选引用是反引号。另请参阅?make.names
关于有效名称。
另请参见 此帖子 关于 dplyr
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")
and the data looks like
a a a b
1 1 2
2 2 3
3 3 4
Following call to select
select(df, 'a a')
gives
Error in abs(ind[ind < 0]) :
non-numeric argument to mathematical function
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?
You may select
the variable by using backticks.
select(df, `a a`)
# a a
# 1 1
# 2 2
# 3 3
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"))
Or in base
R:
names(df)[names(df) == "a a"] <- "a"
For a more thorough description on the use of various quotes, see ?Quoutes
. 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". See also ?make.names
about valid names.
See also this post about renaming in dplyr
这篇关于dplyr:选择包含空格的列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!