Non-redundant version of expand.grid

后端 未结 7 1337
北海茫月
北海茫月 2020-12-01 01:56

The R function expand.grid returns all possible combination between the elements of supplied parameters. e.g.

> expand.grid(c(\"         


        
7条回答
  •  自闭症患者
    2020-12-01 02:41

    here's a very ugly version that worked for me on a similar problem.

    AHP_code = letters[1:10] 
     temp. <- expand.grid(AHP_code, AHP_code, stringsAsFactors = FALSE)
      temp. <- temp.[temp.$Var1 != temp.$Var2, ] # remove AA, BB, CC, etc. 
      temp.$combo <- NA 
      for(i in 1:nrow(temp.)){  # vectorizing this gave me weird results, loop worked fine. 
        temp.$combo[i] <- paste0(sort(as.character(temp.[i, 1:2])), collapse = "")
      }
      temp. <- temp.[!duplicated(temp.$combo),]
      temp. 
    
    

提交回复
热议问题