首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何绕过r中缺少的参数

如何绕过r中缺少的参数
EN

Stack Overflow用户
提问于 2020-09-20 17:43:05
回答 2查看 87关注 0票数 1

我编写了一个以多个向量作为参数的程序。如果输入单个向量作为输入,则该函数提供以下错误消息:

代码语言:javascript
复制
Error in combn(letters[1:length(mylist)], 2) : n < m
Called from: combn(letters[1:length(mylist)], 2)

我知道我为什么会犯这个错误。我使用了一个组合函数组合,它选择了两个向量。我该怎么解决这个问题?我想得到一个输出,即使我的输入是一个单一的向量。在这种情况下,我希望我的代码跳过那些需要两个向量的行,然后运行其余的来获得输出。为了方便起见,我在这里提供了我的全部代码。

代码语言:javascript
复制
### 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)
EN

回答 2

Stack Overflow用户

发布于 2020-09-20 19:38:30

在没有至少两个向量的情况下,可以使用if语句跳过行,这取决于combn

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2020-09-20 19:41:18

您可以根据列表的长度尝试设置combn的第二个参数。

替换

代码语言:javascript
复制
combination<-combn(letters[1:length(mylist)], 2) #choose 2 out of L vectors

出自:

代码语言:javascript
复制
if (length(mylist) < 2) {
  n_elements <- 1
} else {
  n_elements <- 2
}

combination<-combn(letters[1:length(mylist)], n_elements)

编辑

若要使用提供的变量名,请将最后一行更改为:

代码语言:javascript
复制
combination<-combn(names(mylist)[1:length(mylist)], n_elements)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63981911

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档