简体   繁体   English

将命令给出的 position 编号/行号转换为 R 中的行名

[英]convert position number/row number given by which command into rownames in R

The which command in R gives the row number (or position number) but I have assigned names to those row numbers. R 中的 which 命令给出了行号(或 position 号),但我已经为这些行号分配了名称。 How do I convert the row/position number into row names?如何将行/位置编号转换为行名称? Thanks!谢谢!

Example:例子:

 I have a data frame df: [,1] [,2] A 1 2 B 3 4 C 5 6 df1 = which(df[,1] > 0.0).

This gives position number 1,2 and 3. I want to convert it to A, B and C.这给出了 position 编号 1,2 和 3。我想将其转换为 A、B 和 C。

Assuming you have a named vector like in this example you could do:假设你有一个像这个例子中那样的命名向量,你可以这样做:

vector <- c("a", "b", "c")
names(vector) <- c("AA", "BB", "CC")

names(which(vector=="b"))      

[1] "BB"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM