【发布时间】:2018-08-07 19:50:47
【问题描述】:
我正在看这个 data.table 的简短教程
https://www.r-bloggers.com/r-data-table-tutorial-with-50-examples/
但是当作者谈到setkey()时我就卡住了
我会举个例子。我使用 iris 数据库,因此可以轻松复制它
mydata <- as.data.table(iris)
#Change variable names
mydata <- setnames(mydata, c("Sepal.Length","Sepal.Width", "Petal.Length", "Petal.Width", "Species"),
c("sepal_length", "sepal_width", "petal_length", "petal_width", "species"))
现在我将使用一个因子变量和一个数值变量作为键:
setkey(mydata, species, petal_length)
完美地使用它:
> mydata[.("setosa", 1.4)]
sepal_length sepal_width petal_length petal_width species
1: 5.1 3.5 1.4 0.2 setosa
2: 4.9 3.0 1.4 0.2 setosa
3: 5.0 3.6 1.4 0.2 setosa
4: 4.6 3.4 1.4 0.3 setosa
5: 4.4 2.9 1.4 0.2 setosa
6: 4.8 3.0 1.4 0.1 setosa
7: 5.1 3.5 1.4 0.3 setosa
8: 5.2 3.4 1.4 0.2 setosa
9: 5.5 4.2 1.4 0.2 setosa
10: 4.9 3.6 1.4 0.1 setosa
11: 4.8 3.0 1.4 0.3 setosa
12: 4.6 3.2 1.4 0.2 setosa
13: 5.0 3.3 1.4 0.2 setosa
但这会引发错误:
mydata[.("setosa", <1.4)]
Error: inesperado '<' in "mydata[.("setosa", <"
所以我的问题是是否可以在使用setkey 进行搜索时包含 >、=、mydata[.("setosa", <1.4)]
我看了:
R data.table setkey with numeric column
R data.table 1.9.2 issue on setkey
但没有发现任何有用的东西来回答我的问题。
我还阅读了data.table 文档,但没有有用的示例。
任何评论将不胜感激。
【问题讨论】:
-
你似乎在尝试非 equi 连接,搜索一下
-
是的,你是对的。谢谢你:)
标签: r function data.table packages