检索R中特定单元格的行和列名称 [英] retrieve row and column name of particular cell in R
本文介绍了检索R中特定单元格的行和列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以如果我有一个数据框架,如下所示:
So if I have a data frame that looks like:
A B C
rowname1 4.5 4 3.2
rowname2 3 23 9
如何让R给我这个行的名字/列包含特定的数字?
How do I get R to give me the name(s) of the row/columns that contain a particular number?
ie如果我给出值3,它会返回
i.e. if I give the value 3, it gives back
rowname2,A
推荐答案
假设没有重复项,您可以使用
arr.ind
参数:
Assuming no duplicates, you can use which
combined with the arr.ind
argument:
df <- data.frame(matrix(sample(1:100,12), ncol=3))
# X1 X2 X3
# 1 84 58 36
# 2 9 40 92
# 3 100 28 78
# 4 15 98 29
index <- which(df==36, arr.ind=TRUE)
# row col
# [1,] 1 3
如果您必须具有位置的实际行和列名称,那么只需索引到他们适当地:
If you must have the actual row and column names of the location, then just index into them appropriately:
paste(rownames(df)[index[1]], colnames(df)[index[2]], sep=", ")
# [1] "1, X3"
这篇关于检索R中特定单元格的行和列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文