寻找R Studi函数/代码来交叉引用变量

时间:2019-05-27 01:16:51

标签: r

如果有人可以建议我如何在2个变量之间建立关系,我将不胜感激。可能很简单,但是我是R的新手。我无法附加数据集(X2015_11_metropolitan_stop_and_search)外观的照片,但这是我感兴趣的两个变量的总结:

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")

变量“搜索对象”和“结果”的可能值很少。对于那些“结果”值是“涉嫌逮捕”的个别事件,我希望能够看到“搜索对象”值。有可能吗?

如果我需要提供更多信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

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

将返回结果“可疑被逮捕”的“搜索对象”的所有值。通用版本为:

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

还要检查which()函数以返回与您在函数中指定的参数相匹配的数据的索引号。