【问题标题】:how to print the combination in r?如何在r中打印组合?
【发布时间】:2014-01-27 08:19:45
【问题描述】:

我有这样的数据代码包含数据

dat

我想把每一个组合起来打印输出为

A 的输出

V1=>V2V3V4

V2=>V1V3V4

V3=>V1V2V4

V1V2=>V3V4

V1V3=>V2V4

V3V4=>V1V2

V2V4=>V1V3

V2V3V4=>V1

V1V3V4=>V2

V1V2V4=>V3

类似方式B组合 我的代码是

vd<-data.frame()
vd<-data.frame(A=c("V1","V2","V3","V4"),B=c("V1","V2","V3","V4")) 
vf<-length(vd)
i<-1
while(i<=vf)
{
vd<-dat[,i]
leng<-nrow(dat)
selectru<-combn(vd,leng)
fst<-selectru[i]
select<-data.frame()
select<-selectru[selectru[,1]!=selectru[i],]
m<-length(select)
select<-combn(select,m)
snd <-apply(select,2,function(rows) paste0(rows, collapse = ""))
cat(sprintf("\"%s\" =>\"%s\"\n", fst, snd))
i<-i+1
}

此代码不起作用。我不能在单个data.frame 中存储多个组合。这就是问题

【问题讨论】:

    标签: r dataframe apriori


    【解决方案1】:

    您想要的输出似乎有点奇怪(多个组合相同),但我知道可能很难解释您想要什么。下面的代码可能会给你一些启发。它包含所有组合,并在=&gt; 前面显示该组合中未包含的内容。

    dat<-data.frame(A=c("V1","V2","V3","V4"),B=c("V1","V2","V3","V4")) 
    for (h in 1:ncol(dat)) {
      for (i in 1:(nrow(dat)-1)) {
        combinations1 <- combn(nrow(dat), i)
    
        for (j in 1:ncol(combinations1)) {
          k <- combinations1[,j]
          a <- (dat[k,h])
          a <- paste(a, sep="", collapse="") 
          b <-(dat[-k,h])
          b <- paste(b, sep="", collapse="") 
          cat(sprintf("\"%s\" =>\"%s\"\n", a, b))
        }
      }
    }
    

    【讨论】:

    • 真的很抱歉。没有重复的组合。我更正了
    • 它的工作..your great.thanks.Actually没有重复数据集和组合。
    • 是否可以保存这些打印值
    • 是的,可能:d=character() dat\"%s\"\n", a, b) d
    • 啊,是的,那是因为我认为您使用了print(d),它会打印出您必须使用的每个隐藏字符cat(d)writeLines(d)
    猜你喜欢
    • 2023-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多