【问题标题】:Is it possible to identify whether the highest value within a row is unique?是否可以确定一行中的最大值是否唯一?
【发布时间】:2023-03-18 13:38:01
【问题描述】:

我正在尝试比较特定区域内的不同公寓份额(就其大小而言)。数据如下所示:

df <- data.frame (rbind(c(0.33, 0.33, 0.33), c(0.6, 0.4, 0), c(0,0.4,0.6),c(0.25,0.35,0.6)))

names(d) <- c("30-50", "50-70", "70-90")

所有行的总和等于 1(或 100%),每列中的值表示该区域(区域 1 为第 1 行)具有特定大小的公寓的份额

我现在想添加一个附加列,该列给出每行中最大值的列名。因此,我正在使用此代码:

df$highest_value &lt;- names(df)[apply(df, 1, which.max)]

这给出了以下列向量:

[1] "30-50" "30-50" "70-90" "70-90"

对于第 2:4 行,结果是正确的,因为它们包含一个最高值。但是,对于第 1 行,结果不正确,因为它们在该区域的公寓总数中所占的份额相同(全部为 33,333...%),但最高值列给出了第一列的名称。有没有办法确定最高值在行中是否唯一?如果是这样,我希望向量显示这些场景的“混合”。最高值列将如下所示:

[1] "mix" "30-50" "70-90" "70-90"

我希望提供的信息足够。

【问题讨论】:

    标签: r


    【解决方案1】:
    df <- data.frame (rbind(c(0.33, 0.33, 0.33), c(0.6, 0.4, 0), c(0,0.4,0.6),c(0.25,0.35,0.6)))
    
    names(df) <- c("30-50", "50-70", "70-90")
    
    df
    
    # check which values are max?
    df == apply(df, 1, max)
    
    ## count the occurrences of the max values
    rs <- rowSums(df == apply(df, 1, max))
    
    # if there is > 1 occurence of a max value, then "mix", otherwise continue with your previous code
    df$high <- ifelse(rs > 1, "mix", names(df)[apply(df, 1, which.max)])
    
    df
    

    【讨论】:

    • 您至少可以将apply(df, 1, max)do.call(pmax, df) 向量化并保存,这样您就不必计算两次。请记住,边距为 1 的apply 效率非常低,因为它首先强制转换为矩阵
    • 非常感谢它完美运行!!!!除了点赞(因为我还没有评分,所以没有显示)之外,我还有什么方法可以归功于帮助我的人?​​
    【解决方案2】:

    我们可以使用sweep 来比较逐行最大值。如果行中有超过 1 个最大值,我们将返回 "mix",否则返回列名。

    inds <- sweep(df, 1, do.call(pmax, df), `==`)
    ifelse(rowSums(inds) > 1, "mix", names(df)[max.col(inds)])
    #[1] "mix"   "30-50" "70-90" "70-90"
    

    【讨论】:

      【解决方案3】:

      这是另一种解决方案,它每行只循环一次,避免多次apply 调用并使用vapply,通过指定您所期望的输出更安全:

      df <- data.frame (rbind(c(0.33, 0.33, 0.33), c(0.6, 0.4, 0), c(0,0.4,0.6),c(0.25,0.35,0.6)))
      names(df) <- c("30-50", "50-70", "70-90")
      
      # split each row into a list
      list_rows <- asplit(df, 1)
      
      # Loop through each row
      vapply(list_rows, function(x) {
      
        # Count the ocurrence of each number
        count_pos <- table(x)
      
        # If there's any number repreated twice, return mix, otherwise return the max
        if (any(count_pos > 1)) {
          return("mix")
        } else {
          names(x)[which.max(x)]
        }
      }, FUN.VALUE = character(1))
      #> [1] "mix"   "30-50" "70-90" "70-90"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-07
        • 1970-01-01
        • 2015-02-07
        • 2021-10-30
        • 1970-01-01
        • 1970-01-01
        • 2019-10-03
        • 2012-04-26
        相关资源
        最近更新 更多