R语言 必须使用“on=”参数指定要联接的列的数据表中的错误

3phpmpom  于 2023-05-20  发布在  其他
关注(0)|答案(1)|浏览(219)

我写了一个基于条件提取行的函数,如下所示:

ex <- function(x, y){
  Sname <- paste0("A00-",x)
  state <- paste0(x, "_state")
  test1 <- paste0(x, "_test1")
  test2 <- paste0(x, "_test2")
  temp <- dt[c("ID",state,test1,test2)
  temp[,test1] <- ifelse(temp[,state] >= y, temp[,test1] ,"NA")
  head(temp)]
  }

我的表有列

> names(dt)
 [1] "ID"            "A_test1"       "A_test2"       "A_state"     "A_anoo"     
 [6] "B_test1"       "B_test2"       "B_state"       "B_anoo"      "C_test1"      
[11] "C_test2"       "C_state"       "C_anoo"

当我使用我的函数ex(x = "B", y= 0.2)时,我会得到这个错误:

>   temp <- dt[c("ID",state,test1,test2)]
Error in `[.data.table`(dt, c("ID",state,test1,test2), ) : 
  When i is a data.table (or character vector), the columns to join by must be specified using 'on=' argument (see ?data.table), by keying x (i.e. sorted, and, marked as sorted, see ?setkey), or by sharing column names between x and i (i.e., a natural join). Keyed joins might have further speed benefits on very large data due to x being sorted in RAM.

如果有人能帮我,那就太好了。谢谢。

ia2d9nvy

ia2d9nvy1#

您的问题与data.table中的非标准评估有关。

library(data.table)
DT <- data.table(iris)
cols <- c("Species", "Sepal.Length")
DT[cols]
#Error in `[.data.table`(DT, cols) : 
#  When i is a data.table (or character vector), the columns to join by must be specified using 'on=' argument (see ?data.table), by keying x (i.e. sorted, and, marked as sorted, see ?setkey), or by sharing column names between x and i (i.e., a natural join). Keyed joins might have further speed benefits on very large data due to x being sorted in RAM.

DT[, .SD, .SDcols = cols]
#       Species Sepal.Length
#  1:    setosa          5.1
#  2:    setosa          4.9
#  3:    setosa          4.7
#  4:    setosa          4.6
#  5:    setosa          5.0
# ---                       
#146: virginica          6.7
#147: virginica          6.3
#148: virginica          6.5
#149: virginica          6.2
#150: virginica          5.9

我还建议你研究一下数据、表格插图和文档,特别是关于:=(通过引用分配)和fifelse。应避免混合数据表和基础R(子)赋值。

相关问题