如何在 data.table 中引用以数字开头的列名 [英] How to reference column names that start with a number, in data.table
问题描述
如果data.table
中的列名是数字+字符
的形式,例如:4PCS
,5Y
等,如何在 x[i,j]
中将其引用为 j
以便将其解释为不带引号的列名.
If the column names in data.table
are in the form of number + character
, for example: 4PCS
, 5Y
etc, how could this be referenced as j
in x[i,j]
so that it is interpreted as an unquoted column name.
我认为这将解决我原来的问题.我想在data.table"中添加几列,格式为 number + character
.
I assume this would solve mine original problem. I wanted to add several column in 'data.table' which were in the form number + character
.
M <- data.table('4PCS'=1:4,'5Y'=4:1,X5Y=2:5)
> M[,4PCS+5Y]
Error: unexpected symbol in "M[,4PCS"
新列应该是 4PSC
和 5Y
的总和.
The new column should be a sum of 4PSC
and 5Y
.
有没有办法在 data.table
中以不带引号的形式引用它们?如果这些列在 data.table
中引用了 data.frame
的引用逻辑":
Is there a way how to refer to them in data.table
in no quoted form? If these columns are referred in data.table
with the quoted "logic" of data.frame
:
> M[,'5Y',with=FALSE]
5Y
[1,] 4
[2,] 3
[3,] 2
[4,] 1
那么此类参考的功能将受到限制.添加不起作用,因为它在 data.frame
中不起作用:
then there will be a limitation in functionality of such reference. The addition would not work as it does not work in data.frame
:
> M[,'4PCS'+'5Y',with=FALSE]
Error in "4PCS" + "5Y" : non-numeric argument to binary operator
data.table
功能将允许对列进行操作.我想在新的 data.table
逻辑中找到一个解决方案,因此我可以使用它通过 列名引用 来转换列的能力.
The data.table
functionality would allow to operate over the columns. I would like to find a solution in the new data.table
logic hence I can use its ability to transform the columns by column name referencing.
问题是:
如何引用以数字开头的列名,以便 data.table 逻辑理解它是一个列名.
The question is:
How to quote the column name which start with number so that the data.table logic would understand that it is a column name.
推荐答案
我想,这就是你要找的,不确定.data.table
与 data.frame
不同.请查看 快速介绍,然后是常见问题解答(必要时还可参考参考手册).
I think, this is what you're looking for, not sure. data.table
is different from data.frame
. Please have a look at the quick introduction, and then the FAQ (and also the reference manual if necessary).
require(data.table)
dt <- data.table("4PCS" = 1:3, y=3:1)
# 4PCS y
# 1: 1 3
# 2: 2 2
# 3: 3 1
# access column 4PCS
dt[, "4PCS"]
# returns a data.table
# 4PCS
# 1: 1
# 2: 2
# 3: 3
# to access multiple columns by name
dt[, c("4PCS", "y")]
或者,如果您需要访问列并且 not 导致 data.table
而不是向量,那么您可以使用 $
符号:
Alternatively, if you need to access the column and not result in a data.table
, rather a vector, then you can access using the $
notation:
dt$`4PCS` # notice the ` because the variable begins with a number
# [1] 1 2 3
# alternatively, as mnel mentioned under comments:
dt[, `4PCS`]
# [1] 1 2 3
或者,如果您知道列号,您可以使用 [[.]]
访问如下:
Or if you know the column number you can access using [[.]]
as follows:
dt[[1]] # 4PCS is the first column here
# [1] 1 2 3
<小时>
谢谢@joran.我想你正在寻找这个:
Thanks @joran. I think you're looking for this:
dt[, `4PCS` + y]
# [1] 4 4 4
基本上问题是 4CPS
在 R 中不是一个有效的变量名(尝试 4CPS <- 1
,你会得到相同的意外符号"错误).所以要引用它,我们必须使用反引号 (compare`4CPS` <- 1
)
Fundamentally the issue is that 4CPS
is not a valid variable name in R (try 4CPS <- 1
, you'll get the same "Unexpected symbol" error). So to refer to it, we have to use backticks (compare`4CPS` <- 1
)
这篇关于如何在 data.table 中引用以数字开头的列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!