我编写了一个以多个向量作为参数的程序。如果输入单个向量作为输入,则该函数提供以下错误消息:
Error in combn(letters[1:length(mylist)], 2) : n < m
Called from: combn(letters[1:length(mylist)], 2)我知道我为什么会犯这个错误。我使用了一个组合函数组合,它选择了两个向量。我该怎么解决这个问题?我想得到一个输出,即使我的输入是一个单一的向量。在这种情况下,我希望我的代码跳过那些需要两个向量的行,然后运行其余的来获得输出。为了方便起见,我在这里提供了我的全部代码。
### Function to check whether two sets a,b are incomparable
incomparable <- function(a,b){
cond1 <- sum(a>b) > 0 # Returns true is if there is at least one case such that a_i is 1 and b_i is 0
cond2 <- sum(b>a) > 0 # True if at least one case such that one case with b_i is 1 and a_i is 0
ifelse (length(a)== length(b), cond1*cond2, "Strings are not of the same length")
}
incomparable(a,b)
### Function to get subsets of a binary vector
binary_subset<-function(a){
a_seq = lapply(a, seq, 0) # keep 0s as 0, make 1s c(1, 0)
subset=do.call(expand.grid, a_seq)
colnames(subset)=(1:length(a))
return(subset)
}
#### Function to generate all lower-order interaction terms correspond to interaction terms a,b,c...upto any arbitrary number L (including terms a, b, c...L)
all_lower_order_interactions<-function(...){
mylist <- list(...)
combination<-combn(letters[1:length(mylist)], 2) #choose 2 out of L vectors
check_incomparable<-0
for (j in 1:ncol(combination)){
check_incomparable[j]<- (incomparable(get(combination[1,j]), get(combination[2,j])))
}
check_incomparable
if(all(check_incomparable>0)==FALSE) {stop( "at least one of the interaction terms is a special case (or a subset) of another term.")}
interactions_abc <- do.call("rbind", lapply(mylist, binary_subset))
interactions_no_duplicate <- unique(interactions_abc[1:length(mylist[[1]])])
rownames(interactions_no_duplicate) <- 1:nrow(interactions_no_duplicate)
interactions_no_duplicate
}
a<-c(0,1,0)
b<-c(0,0,1)
all_lower_order_interactions(a)发布于 2020-09-20 19:38:30
在没有至少两个向量的情况下,可以使用if语句跳过行,这取决于combn:
all_lower_order_interactions <- function(...) {
mylist <- list(...)
if (length(mylist) >= 2) {
{
combination <-
combn(letters[1:length(mylist)], 2) #choose 2 out of L vectors
}
check_incomparable <- 0
for (j in 1:ncol(combination)) {
check_incomparable[j] <-
(incomparable(get(combination[1, j]), get(combination[2, j])))
}
check_incomparable
if (all(check_incomparable > 0) == FALSE) {
stop("at least one of the interaction terms is a special case (or a subset) of another term.")
}
}
interactions_abc <-
do.call("rbind", lapply(mylist, binary_subset))
interactions_no_duplicate <-
unique(interactions_abc[1:length(mylist[[1]])])
rownames(interactions_no_duplicate) <-
1:nrow(interactions_no_duplicate)
interactions_no_duplicate
}
a <- c(0, 1, 0)
b <- c(0, 0, 1)
all_lower_order_interactions(a)
1 2 3
1 0 1 0
2 0 0 0发布于 2020-09-20 19:41:18
您可以根据列表的长度尝试设置combn的第二个参数。
替换
combination<-combn(letters[1:length(mylist)], 2) #choose 2 out of L vectors出自:
if (length(mylist) < 2) {
n_elements <- 1
} else {
n_elements <- 2
}
combination<-combn(letters[1:length(mylist)], n_elements)编辑
若要使用提供的变量名,请将最后一行更改为:
combination<-combn(names(mylist)[1:length(mylist)], n_elements)https://stackoverflow.com/questions/63981911
复制相似问题