简体   繁体   English

根据r中的单元格值返回行和列的名称

[英]return row and column name based on cell value in r

Appreciate any help on this, I am relatively new to R and stackoverflow. 感谢任何帮助,我对R和stackoverflow还是比较新的。

Here is some sample code to work with for my problem, it is from a database of injured workers. 这是一些针对我的问题的示例代码,它来自受伤工人的数据库。

Area <- c("Connecticut", "Maine", "Massachusetts", "New Hampshire")
X2004 <- c(0,1,4,1)
X2005 <- c(1,0,6,2) 
df1 <- data.frame(Area, X2004, X2005) 

I would like to write a simple script that takes a number of injured workers and returns the "Area" and year in which a specific number of officers were killed. 我想编写一个简单的脚本,其中包含许多受伤的工人,并返回“地区”和年份,其中特定数量的官员被杀。 Eg "6" would return X2005 and Massachusetts, and 1 would return X2004 Connecticut and X2005 Maine. 例如,“ 6”将返回X2005和马萨诸塞州,“ 1”将返回X2004 Connecticut和X2005 Maine。 Along the lines of Vlookup in Excel. 在Excel中遵循Vlookup。

The closer to base R the better, I am doing this as part of an exercise on indexing. 离R越近越好,我正在以此作为索引练习的一部分。 I haven't found any solutions with tidyverse either. 我也没有用tidyverse找到任何解决方案。

Thanks in advance for your help, I'm sure there is a clear solution just beyond my reach! 在此先感谢您的帮助,我敢肯定有一个明确的解决方案超出了我的能力范围!

You can using melt from reshape 您可以使用reshape melt

newdf1=reshape::melt(df1)
newdf1[newdf1$value==6,c('Area','variable')]
           Area variable
7 Massachusetts    X2005

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

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