gpt4 book ai didi

r - 选择两列之间具有最大值的列值

转载 作者:行者123 更新时间:2023-12-02 18:25:04 25 4
gpt4 key购买 nike

我有一个名为

的数据框
tt <- structure(list(IID1 = c("035-0028-0151526", "036-1002-0282844", 
"950-0001-0081329", "951-1000-0084436", "954-0009-0337439", "954-0011-0379455",
"955-0017-0354237", "6011039", "6011039", "6011039", "6011039",
"6205526", "A1858", "A2159"), IID2 = c("035-0028-0157134", "036-1002-0305628",
"950-0001-0100509", "951-1000-0084537", "954-0009-0337641", "954-0011-0379657",
"955-0017-0363137", "6205526", "6215777", "A1864", "A2267", "A1864",
"A1864", "A2267"), nIID1 = c(1, 1, 1, 1, 1, 1, 1, 4, 4, 4, 4,
1, 1, 1), nIID2 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 2, 3, 3, 2)), row.names = c(NA,
-14L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x38b6d80>)

我想从 IID1IID2 中提取值并将其放入 selected 列中。如果nIID1 == nIID2,我想要来自IID1的值,否则,我想要来自具有最大IID的列的值。例如,如果 nIID1 的值高于 nIID2,那么我需要 IID1 的值。有人可以指导我解决方案吗?谢谢!

结果应如下所示。

               IID1             IID2 nIID1 nIID2         selected
1: 035-0028-0151526 035-0028-0157134 1 1 035-0028-0151526
2: 036-1002-0282844 036-1002-0305628 1 1 036-1002-0282844
3: 950-0001-0081329 950-0001-0100509 1 1 950-0001-0081329
4: 951-1000-0084436 951-1000-0084537 1 1 951-1000-0084436
5: 954-0009-0337439 954-0009-0337641 1 1 954-0009-0337439
6: 954-0011-0379455 954-0011-0379657 1 1 954-0011-0379455
7: 955-0017-0354237 955-0017-0363137 1 1 955-0017-0354237
8: 6011039 6205526 4 1 6011039
9: 6011039 6215777 4 1 6011039
10: 6011039 A1864 4 3 6011039
11: 6011039 A2267 4 2 6011039
12: 6205526 A1864 1 3 A1864
13: A1858 A1864 1 3 A1864
14: A2159 A2267 1 2 A2267

最佳答案

我们可以通过max.col获取列索引,通过cbind获取行索引(.I),从cbinded IID1、IID2 并将 (:=) 分配给“选定”

tt[, selected := cbind(IID1, IID2)[cbind(.I, max.col(.SD, "first"))],
.SDcols = nIID1:nIID2]

-输出

> tt
IID1 IID2 nIID1 nIID2 selected
1: 035-0028-0151526 035-0028-0157134 1 1 035-0028-0151526
2: 036-1002-0282844 036-1002-0305628 1 1 036-1002-0282844
3: 950-0001-0081329 950-0001-0100509 1 1 950-0001-0081329
4: 951-1000-0084436 951-1000-0084537 1 1 951-1000-0084436
5: 954-0009-0337439 954-0009-0337641 1 1 954-0009-0337439
6: 954-0011-0379455 954-0011-0379657 1 1 954-0011-0379455
7: 955-0017-0354237 955-0017-0363137 1 1 955-0017-0354237
8: 6011039 6205526 4 1 6011039
9: 6011039 6215777 4 1 6011039
10: 6011039 A1864 4 3 6011039
11: 6011039 A2267 4 2 6011039
12: 6205526 A1864 1 3 A1864
13: A1858 A1864 1 3 A1864
14: A2159 A2267 1 2 A2267

或者另一个选项是fcase

tt[, selected := fcase(nIID1 >= nIID2, IID1, nIID1 < nIID2, IID2)]

编辑:按照@Marco_CH的建议

关于r - 选择两列之间具有最大值的列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70340182/

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