代码之家  ›  专栏  ›  技术社区  ›  ch-pub

ddply函数忽略迭代器

  •  1
  • ch-pub  · 技术社区  · 8 年前

    iris$Petal.Length iris$Species

    分位数的数量和值是动态的,所以最终我会尝试通过概率循环或以某种方式将其矢量化。这是我的矢量化尝试,但不太奏效。

    rm(list = ls())
    
    require(plyr)
    
    myDat <- iris
    
    myProbs <- c(0, 0.15, 0.5, 1)
    
    # This doesn't return the DF I'm looking for (where probabilities/names are identified)
    petals_by_species <- ddply(myDat, "Species", summarize, Quantiles = quantile(Petal.Length, probs = myProbs))
    petals_by_species
    

    enter image description here

    我尝试了一些黑客的工作,将结果合并成一些广泛的格式,如:

    rm(list = ls())
    
    require(plyr)
    
    myDat <- iris
    
    myProbs <- c(0, 0.15, 0.5, 1)
    
    # So, I loop through the probabilities and combine.
    for(i in 1:length(myProbs)){
    
      temp <- ddply(myDat, "Species", summarize, Quantiles = quantile(Petal.Length, probs = myProbs[i]))
    
      names(temp) <- c("Species", paste0("Prob ", myProbs[i]))
    
      if(i == 1){
        petals_by_species <- temp
      } else {
        petals_by_species <- merge(petals_by_species, temp)
      }
    }
    
    petals_by_species
    

    此输出完全令人困惑……列名正确,但值不正确(每列都重复出现)。

    enter image description here

    以上列均未返回正确的值。

    很明显,我并没有用正确的方式来做这件事。但现在我的好奇心被激发了,

    require(plyr)
    
    myDat <- iris
    
    myProbs <- c(0, 0.15, 0.5, 1)
    
    intendedOutput <- ddply(myDat, "Species", summarize, Quantiles = quantile(Petal.Length, probs = myProbs[1]))
    intendedOutput
    
    i = 1
    unintendedOutput <- ddply(myDat, "Species", summarize, Quantiles = quantile(Petal.Length, probs = myProbs[i]))
    unintendedOutput
    

    enter image description here

    我怎样才能得到 ddply 以我期望的方式识别迭代器?有不同的吗 plyr daply 没有成功。

    谢谢

    1 回复  |  直到 8 年前
        1
  •  0
  •   ch-pub    8 年前

    这是一张与哈德利的单独信件中的票:

    rm(list = ls())
    
    require(plyr)
    
    myDat <- iris
    
    myProbs <- c(0, 0.15, 0.5, 1)
    
    # This doesn't return the DF I'm looking for (where probabilities/names are identified)
    petals_by_species <- ddply(myDat, "Species", summarize, Quantiles = quantile(Petal.Length, probs = myProbs), probs = myProbs)
    petals_by_species
    

    然后我的输出是长格式的,并报告输入,如下所示:

    enter image description here