which()函数返回给定条件的行索引。是否有类似于哪个()可以识别给定条件的列索引?或者,有没有其他方法可以获得它?
假设我们有以下数据框。 5个不同城市的小时温度,其中城市名称是列,小时(00到23)是行。如何找出哪个城市记录了最高温度,以及何时记录?
答案 0 :(得分:1)
使用这样的样本数据(最多24个):
Mobile Juneau Phoenix Little Rock Los Angeles
1 9.161109 6.561874 7.900787 3.863214 7.2799115
2 5.597790 3.267891 3.895087 7.589563 0.5756907
3 6.279322 3.578783 5.273761 8.076495 6.2456569
4 3.895283 4.321506 2.665034 2.755444 9.8913468
5 5.889698 5.690749 8.879107 8.233134 3.2684326
6 7.811671 2.033278 1.528914 7.719961 1.3486213
在调用arr.ind=TRUE
时使用which
给出行和列(此处只有一个):
> which(df==max(df),arr.ind=TRUE)
row col
[1,] 14 5
接下来,使用rownames和colnames,您应该能够获得城市名称和小时(有关详细信息,请参阅?which
)。
测试数据:
df <- structure(list(Mobile = c(9.16110935388133, 5.59779000002891,
6.27932160394266, 3.89528321102262, 5.88969846023247, 7.81167097389698,
1.83368496363983, 4.47858408326283, 5.93715427909046, 3.50760932080448,
9.44435691693798, 6.31296712905169, 5.32727922545746, 7.65801254892722,
8.5059278132394, 4.28985294653103, 2.05249830847606, 7.6365390396677,
9.77846014779061, 8.94446840044111, 0.083497145678848, 7.10133319022134,
0.554085657931864, 3.60153668094426), Juneau = c(6.56187414424494,
3.26789108337834, 3.57878275448456, 4.32150553911924, 5.69074853556231,
2.033277512528, 7.18786624493077, 6.14864403847605, 8.68574564578012,
1.30817822180688, 1.01500122342259, 8.8916674698703, 5.60441857203841,
8.86488395975903, 6.64872422581539, 0.683707664720714, 0.72935079690069,
6.96065725525841, 1.50260855443776, 9.30632123956457, 8.09938014484942,
3.17607591627166, 1.66992667596787, 6.5417457325384), Phoenix = c(7.90078683523461,
3.89508724678308, 5.27376119513065, 2.66503410181031, 8.87910738587379,
1.52891436824575, 1.83640674687922, 7.62884735595435, 1.86433888971806,
6.2539599230513, 0.442896636668593, 8.08971904451028, 3.22157387156039,
1.07892997562885, 5.32610179157928, 5.71723570115864, 8.49908272270113,
9.22176177380607, 5.49865348497406, 2.0716791623272, 2.75534275220707,
9.54129027202725, 0.547004980035126, 0.0254918588325381), `Little Rock` = c(3.86321445228532,
7.58956262841821, 8.0764949019067, 2.75544387986884, 8.23313429951668,
7.71996134659275, 6.78180204005912, 2.48242149595171, 5.22057399619371,
8.16672697663307, 2.18510434962809, 8.52984978584573, 4.81410115258768,
9.26382135832682, 5.73062423849478, 4.68517286004499, 4.98139521572739,
2.03285798197612, 1.81552906520665, 1.28075355896726, 7.3631735984236,
9.16532466653734, 3.6388257634826, 6.7194988601841), `Los Angeles` = c(7.27991148363799,
0.575690739788115, 6.24565688893199, 9.89134680945426, 3.26843255432323,
1.34862127713859, 0.358565023634583, 9.37912961700931, 1.25442329328507,
1.73299844376743, 9.95316742919385, 7.42675306508318, 6.60283848410472,
9.98386316467077, 5.81940121715888, 2.53246366977692, 4.47731937048957,
0.9123918088153, 7.94102079933509, 7.45049795368686, 1.02275986224413,
2.63348019681871, 1.82409069268033, 7.71245903801173)), row.names = c(NA,
-24L), class = "data.frame", .Names = c("Mobile", "Juneau", "Phoenix",
"Little Rock", "Los Angeles"))