首页 > 解决方案 > 寻找 R Studi 函数/代码来交叉引用变量

问题描述

如果有人能就如何建立两个变量之间的关系向我提供建议,我将不胜感激。这可能非常简单,但我是 R 新手。我无法附上我的数据集 (X2015_11_metropolitan_stop_and_search) 的照片,但这是我感兴趣的两个变量的 dput:

dput(X2015_11_metropolitan_stop_and_search$Object of search)
c("Offensive weapons", "Controlled drugs", "Controlled drugs", "Fireworks", "Controlled drugs", "Stolen goods", "Controlled drugs", "Controlled drugs", "Offensive weapons", "Controlled drugs", "Fireworks", "Controlled drugs", "Controlled drugs", "Evidence of offences under the Act", "Controlled drugs", "Controlled drugs", "Controlled drugs", "Controlled drugs", "Controlled drugs", "Controlled drugs", "Controlled drugs", "Controlled drugs", "Controlled drugs", "Controlled drugs", "Controlled drugs", "Controlled drugs", "Offensive weapons", "Stolen goods", "Controlled drugs", "Controlled drugs", "Offensive weapons", "Offensive weapons", "Controlled drugs", "Controlled drugs", "Controlled drugs", "Controlled drugs", "Controlled drugs" )
dput(X2015_11_metropolitan_stop_and_search$Outcome)
c("Suspect arrested", "Nothing found - no further action", "Suspect arrested", 
"Nothing found - no further action", "Nothing found - no further action", 
"Nothing found - no further action", "Offender given drugs possession warning", 
"Nothing found - no further action", "Offender given drugs possession warning", 
"Nothing found - no further action", "Nothing found - no further action", 
"Suspect arrested", "Suspect arrested", "Offender given drugs possession warning", 
"Suspect arrested", "Suspect arrested", "Nothing found - no further action", 
"Nothing found - no further action", "Suspect arrested", "Nothing found - no further action", 
"Nothing found - no further action", "Suspect arrested", "Suspect arrested", 
"Nothing found - no further action", "Nothing found - no further action", 
"Nothing found - no further action", "Nothing found - no further action", 
"Suspect arrested", "Nothing found - no further action", "Nothing found - no further action", 
"Nothing found - no further action", "Nothing found - no further action", 
"Nothing found - no further action", "Nothing found - no further action", 
"Nothing found - no further action", "Nothing found - no further action", 
"Suspect arrested", "Nothing found - no further action", "Nothing found - no further action", 
"Suspect arrested", "Nothing found - no further action", "Nothing found - no further action", 
"Nothing found - no further action", "Suspect arrested", "Suspect arrested", 
"Nothing found - no further action", "Nothing found - no further action", 
"Nothing found - no further action", "Offender given drugs possession warning", 
"Suspect arrested", "Offender given penalty notice", "Nothing found - no further action")

变量“搜索对象”和“结果”有少量可能的值。我希望能够看到那些“结果”值为“嫌疑人被捕”的个别事件的“搜索对象”值。那可能吗?

如果我需要包含更多信息,请告诉我。

标签: r

解决方案


您可以尝试以下方法:

X2015_11_metropolitan_stop_and_search$Object of search[X2015_11_metropolitan_stop_and_search$Outcome == "Suspect arrested"]

这将返回结果为“嫌疑人被捕”的“搜索对象”的所有值。一个通用版本是:

data$column1[data$column2 == "dataValue"]

另请查看 which() 函数以返回符合您在函数中指定的参数的数据的索引号。


推荐阅读