dplyr:非标准列名(空格,标点符号,以数字开头) [英] dplyr: nonstandard column names (white space, punctuation, starts with numbers)

查看:62
本文介绍了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")

数据看起来像

  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:

  1. names(df)[1]<-"a"
  2. select(df,a = 1)
  3. 选择(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

但是,如果您的主要目的是重命名列,则可以在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆