gpt4 book ai didi

r - 显示最大值是否移动到其他列

转载 作者:行者123 更新时间:2023-12-01 12:38:20 26 4
gpt4 key购买 nike

我准备了一些示例数据,让我们来看看它:

> dput(example1)
structure(list(Fr1 = c(0.2, 0, 0, 0, 0, 0), Fr2 = c(0.7, 0, 0,
0, 0, 0), Fr3 = c(1, 0.35, 0, 0, 0, 0), Fr4 = c(0.1, 1, 0, 0,
0.5, 0), Fr5 = c(0, 0.4, 0, 0, 1, 0), Fr6 = c(0, 0, 0, 0, 0.3,
0), Fr7 = c(0, 0, 0, 0.7, 0, 0), Fr8 = c(0, 0, 0, 1, 0, 0), Fr9 = c(0,
0, 0, 1, 0, 0), Fr10 = c(0, 0, 0, 0.65, 0, 0.7), Fr11 = c(0,
0, 0, 0.2, 0, 1)), .Names = c("Fr1", "Fr2", "Fr3", "Fr4", "Fr5",
"Fr6", "Fr7", "Fr8", "Fr9", "Fr10", "Fr11"), row.names = c("Mazda RX4",
"Mazda RX4 Wag", "Datsun 710", "Hornet 4 Drive", "Hornet Sportabout",
"Valiant"), class = "data.frame")

> dput(example2)
structure(list(Fr1 = c(1, 0, 0, 0, 0, 0), Fr2 = c(0.7, 0, 0,
0, 0, 0), Fr3 = c(0.2, 0, 0, 0, 0, 0), Fr4 = c(0.1, 0, 0, 0,
0.5, 0), Fr5 = c(0, 0.1, 0, 0, 1, 0), Fr6 = c(0, 0, 0, 0, 0.3,
0), Fr7 = c(0, 0.8, 0, 0.7, 0, 0), Fr8 = c(0, 1, 0, 1, 0, 0),
Fr9 = c(0, 0.3, 0, 1, 0, 0), Fr10 = c(0, 0, 0, 0.65, 0, 0.7
), Fr11 = c(0, 0, 0, 0.2, 0, 1)), .Names = c("Fr1", "Fr2",
"Fr3", "Fr4", "Fr5", "Fr6", "Fr7", "Fr8", "Fr9", "Fr10", "Fr11"
), row.names = c("Mazda RX4", "Mazda RX4 Wag", "Datsun 710",
"Hornet 4 Drive", "Hornet Sportabout", "Valiant"), class = "data.frame")

所以,我们有 2 个数据框,我想进行比较。正如您在所有单元格中看到的那样,只有从 0 到 1 的数字。数字 1 是最大值,它必须在每行中至少出现一次。对我来说重要的是我可以在哪一列中找到最大值并比较它是否在另一个数据框中的同一列中。

示例 1:
    Fr1 Fr2  Fr3 Fr4 Fr5 Fr6 Fr7 Fr8 Fr9 Fr10 Fr11
Mazda RX4 0.2 0.7 1.00 0.1 0.0 0.0 0.0 0 0 0.00 0.0
Mazda RX4 Wag 0.0 0.0 0.35 1.0 0.4 0.0 0.0 0 0 0.00 0.0
Datsun 710 0.0 0.0 0.00 0.0 0.0 0.0 0.0 0 0 0.00 0.0
Hornet 4 Drive 0.0 0.0 0.00 0.0 0.0 0.0 0.7 1 1 0.65 0.2
Hornet Sportabout 0.0 0.0 0.00 0.5 1.0 0.3 0.0 0 0 0.00 0.0
Valiant 0.0 0.0 0.00 0.0 0.0 0.0 0.0 0 0 0.70 1.0

示例2:
                  Fr1 Fr2 Fr3 Fr4 Fr5 Fr6 Fr7 Fr8 Fr9 Fr10 Fr11
Mazda RX4 1 0.7 0.2 0.1 0.0 0.0 0.0 0 0.0 0.00 0.0
Mazda RX4 Wag 0 0.0 0.0 0.0 0.1 0.0 0.8 1 0.3 0.00 0.0
Datsun 710 0 0.0 0.0 0.0 0.0 0.0 0.0 0 0.0 0.00 0.0
Hornet 4 Drive 0 0.0 0.0 0.0 0.0 0.0 0.7 1 1.0 0.65 0.2
Hornet Sportabout 0 0.0 0.0 0.5 1.0 0.3 0.0 0 0.0 0.00 0.0
Valiant 0 0.0 0.0 0.0 0.0 0.0 0.0 0 0.0 0.70 1.0

在此示例中,我仅对第一行和第二行进行了区分,以使其更易于工作,但在我的真实数据中,所有 3000 行可能都存在差异。正如我提到的,每行中可能有超过 1 个“最大值”,但通常不超过 2 个 - 数字 1 出现两次。

作为输出,我需要行的名称以及最大值是否移动(是)或在同一列中(否)。有可能吗?

为了向您展示这些数据集中的两行不同:

示例 1:
Example 1

示例 2
Example 2

编辑:

真实数据:
structure(list(X10 = c(0, 0, 0, 0, 0, 0), X33.95 = c(0, 0, 0, 
0, 0, 0), X58.66 = c(0, 0, 0, 0, 0, 0.164279901), X84.42 = c(0,
0, 0, 0, 0, 0), X110.21 = c(0.04925863, 0, 0, 0, 0, 0), X134.16 = c(0.4981384,
0, 0, 0, 0, 0), X164.69 = c(1, 0, 1, 0, 0, 0), X199.1 = c(0.367449159,
0, 0, 0, 1, 0), X234.35 = c(0.19587217, 0, 0, 0.96458515, 0.93848979,
0), X257.19 = c(0, 0, 0, 0.77155521, 0, 0), X361.84 = c(0, 0,
1, 0.76396661, 0, 0), X432.74 = c(0, 0, 0.81609991, 0.33773581,
0, 0), X506.34 = c(0, 0, 0.81609991, 0.1390399, 0, 0), X581.46 = c(0,
0, 0.96019504, 0.86300673, 0, 0), X651.71 = c(0, 0, 0, 0.77764596,
0, 0), X732.59 = c(0, 0, 1, 0.45950141, 0, 0), X817.56 = c(0,
0, 0, 0.14639304, 0, 0), X896.24 = c(0, 0.4013747, 0, 0.800272,
0, 0), X971.77 = c(0, 0.32393615, 0, 0.74026623, 0, 0), X1038.91 = c(0,
0.4168461, 0, 0.6808022, 0, 0), NA..1 = c(0, 0.8750537, 0, 1,
0, 0), NA..2 = c(0, 1, 0, 0, 0, 0), NA..3 = c(0, 0.6069765, 0,
1, 0, 0), NA..4 = c(0, 0.53831215, 0, 0.65073089, 0, 0)), .Names = c("X10",
"X33.95", "X58.66", "X84.42", "X110.21", "X134.16", "X164.69",
"X199.1", "X234.35", "X257.19", "X361.84", "X432.74", "X506.34",
"X581.46", "X651.71", "X732.59", "X817.56", "X896.24", "X971.77",
"X1038.91", "NA..1", "NA..2", "NA..3", "NA..4"), row.names = c(NA,
6L), class = "data.frame")

新编辑:

我不明白...:
> apply(alcr_ready,2,is.numeric)
NA. X10 X33.95 X58.66 X84.42 X110.21 X134.16 X164.69 X199.1 X234.35 X257.19 X361.84 X432.74
FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
X506.34 X581.46 X651.71 X732.59 X817.56 X896.24 X971.77 X1038.91 NA..1 NA..2 NA..3 NA..4
FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

再次检查:
> class(alcr_ready[2,2])
[1] "numeric"

再次编辑:
'data.frame':   2188 obs. of  25 variables:
$ NA. : Factor w/ 2890 levels "AT1G01050","AT1G01080",..: 1 2 3 4 5 6 7 10 11 12 ...
$ X10 : num 0 0 0 0 0 0 0 0 0 0 ...
$ X33.95 : num 0 0 0 0 0 0 0 0 0 0 ...
$ X58.66 : num 0 0 0 0 0 ...
$ X84.42 : num 0 0 0 0 0 0 0 0 0 0 ...
$ X110.21 : num 0.0493 0 0 0 0 ...
$ X134.16 : num 0.498 0 0 0 0 ...
$ X164.69 : num 1 0 1 0 0 0 0 0 0 0 ...
$ X199.1 : num 0.367 0 0 0 1 ...
$ X234.35 : num 0.196 0 0 0.965 0.938 ...
$ X257.19 : num 0 0 0 0.772 0 ...
$ X361.84 : num 0 0 1 0.764 0 ...
$ X432.74 : num 0 0 0.816 0.338 0 ...
$ X506.34 : num 0 0 0.816 0.139 0 ...
$ X581.46 : num 0 0 0.96 0.863 0 ...
$ X651.71 : num 0 0 0 0.778 0 ...
$ X732.59 : num 0 0 1 0.46 0 ...
$ X817.56 : num 0 0 0 0.146 0 ...
$ X896.24 : num 0 0.401 0 0.8 0 ...
$ X971.77 : num 0 0.324 0 0.74 0 ...
$ X1038.91: num 0 0.417 0 0.681 0 ...
$ NA..1 : num 0 0.875 0 1 0 ...
$ NA..2 : num 0 1 0 0 0 0 0 0 0 0 ...
$ NA..3 : num 0 0.607 0 1 0 ...
$ NA..4 : num 0 0.538 0 0.651 0 ...

尝试代码:
> indx1 <- max.col(alcr_ready, 'first')==max.col(tps_ready, 'first')
Warning messages:
1: In max.col(alcr_ready, "first") : NAs introduced by coercion
2: In max.col(tps_ready, "first") : NAs introduced by coercion
> indx2 <- max.col(alcr_ready, 'last')==max.col(tbl_tps, 'last')
Warning messages:
1: In max.col(alcr_ready, "last") : NAs introduced by coercion
2: In max.col(tbl_tps, "last") : NAs introduced by coercion
3: In max.col(alcr_ready, "last") == max.col(tbl_tps, "last") :
longer object length is not a multiple of shorter object length

最佳答案

或许

 c('NO', 'YES')[(max.col(example1, 'first')==max.col(example2, 'first'))+1]
# [1] "NO" "NO" "YES" "YES" "YES" "YES"

YES表示 max移到另一列,应该是相反的
c('YES', 'NO')[(max.col(example1, 'first')==max.col(example2, 'first'))+1]
# [1] "YES" "YES" "NO" "NO" "NO" "NO"

如果有可能达到 2 1每行
 indx1 <- max.col(example1, 'first')==max.col(example2, 'first')
indx2 <- max.col(example1, 'last')==max.col(example2, 'last')
c('YES', 'NO')[(indx1|indx2)+1]

关于r - 显示最大值是否移动到其他列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27504490/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com