1

出于好奇,我如何直接在 R 中调用 '[.class' 函数?

我知道我可以做到:

test <- c(2,4,6)
test[2]

但是可以直接指定类吗?如果是这样,怎么办?

'[.numeric<-'(test , 2)
'[.numeric'(test , 2)

我试过这些,但他们回来了

Error: could not find function "[.numeric"
4

1 回答 1

5

[是一个内部泛型,这意味着调度发生在 C 中,而基本类型(如数字)在 R 中没有 S3 方法。这就是为什么没有 [.numericor [<-.numeric

目前尚不清楚你想要什么,但对于你的例子,你可以做

test <- 1:3
`[`(test, 2)
`[<-`(test, 2, 3)
于 2013-08-15T15:08:53.403 回答