[英]use of apply in a function that accepts vector, matrix, or data.frame as an argument
I'm trying to write a function that accepts (amongst other things) an argument that might be either a vector
, a matrix
, or a data.frame
. 我正在尝试编写一个函数,该函数接受(除其他事项外)一个参数,该参数可以是
vector
, matrix
或data.frame
。
set.seed(101)
MyT <- seq(0, 1, 0.1)
S <- sample(seq(0, 1, 0.01), 15, replace = T)
L1 <- sample(c(0,1), 15, replace = T)
L2 <- sample(c(0,1), 15, replace = T)
M1 <- as.matrix(L1)
M2 <- as.matrix(L1, L2)
D1 <- data.frame(L1)
D2 <- data.frame(L1, L2)
I'd like to write a function that is generic enough to accept L1
(or L2
), M1
, M2
, D1
, or D2
as an argument, which I'll call myArg
. 我想编写一个通用性足以接受
L1
(或L2
), M1
, M2
, D1
或D2
作为参数的函数,我将其称为myArg
。 Inside the function, I want to do something like: 在函数内部,我想执行以下操作:
sapply(MyT, function(t) { apply(D2[S > t, ], 2, sum) })
The line above works well. 上面的行效果很好。 The issue is that I need to make it generic, like:
问题是我需要使其通用,例如:
sapply(MyT, function(t) { apply(myArg[S > t, ], 2, sum) })
However, this code would fail, for instance, if myArg
is L1
or L2
. 但是,例如,如果
myArg
为L1
或L2
,则此代码将失败。 In this particular case, because I am trying to subset a vector illegally, the error message will read incorrect number of dimensions
. 在这种情况下,由于我试图非法子集矢量,因此错误消息将读取
incorrect number of dimensions
。 If I fix the subset issue, then I run into the dim(X) must have a positive length
issue, because I can't use apply
on a vector. 如果我解决了子集问题,那么我碰到
dim(X) must have a positive length
问题,因为我不能在向量上使用apply
。 And so on and so forth... 等等等等...
So, I've landed on trying to do the following: 因此,我着手尝试执行以下操作:
sapply( MyT, function(t) {
if (length(dim(myArg)) == 0) sum(myArg[S > t]) # for vector
else if (dim(myArg)[2] == 1) sum(myArg[S > t, ]) # for single-column matrix or data.frame
else sapply(myArg[S > t, ], 2, sum) # for multi-column matrix or data.frame
})
I expected this to work, but I'm still getting the Error in apply(myArg[S > t, ], 2, sum) : dim(X) must have a positive length
when I use M2
as myArg
. 我期望这可以工作,但是我仍然遇到
Error in apply(myArg[S > t, ], 2, sum) : dim(X) must have a positive length
当我使用M2
作为myArg
时, Error in apply(myArg[S > t, ], 2, sum) : dim(X) must have a positive length
。
I'm confused because dim(M2)
yields [1] 15 2
. 我很困惑,因为
dim(M2)
产生[1] 15 2
。
Curious if anyone can shed light on why I get this error when I use M2
as myArg
好奇是否有人可以阐明为什么当我使用
M2
作为myArg
时为什么会出现此错误
R has an object framework. R有一个对象框架。 It has three of them, in fact (four if you count R6, five if you count proto).
实际上,它有3个(如果计算R6,则为4个,如果计算proto,则为5个)。 So why not make use of them?
那么为什么不利用它们呢?
myFunc <- function(x, ...)
UseMethod("myFunc")
# don't use apply() on data frames unless you know what you're doing
myFunc.data.frame <- function(x, S, t, ...)
sapply(x[S > t, ], myFunc.default)
myFunc.matrix <- function(x, S, t, ...)
apply(x[S > t, ], 2, myFunc.default)
myFunc.default <- function(x, S=1, t=0, ...)
sum(x[S > t])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.