-1

I have a list full of generic drugs, and a user makes an input to check if this drug exists in my list (scraped from Medline). The program checks if this drug exists in my list . The thing is that i checked the drug existence with:

vast<-user_comp %in% medline_comps[1]

user_comp being the user drug input and medline_comps the list of drug names which was always true.

And the variable "vast" was always true because for example "Phenylephrine" exists in medline_comps. The thing is i haven's changed anything in the code and suddenly vast started returning FALSE, i have tried rebooting R, clearing every object from the workspace and without any change %in% started returning FALSE instead of TRUE. I double checked the existense of the drug in the list, i even copy pasted the drug in the list and put it in the user input and is still returning FALSE when in the past was returning TRUE.

Is there a special reason why R could give you different results when you havent changed anything in the code?

An example of my list is:

medline_comps[1]

[[1]]
[1] "Phenylephrine"               "Pyrethrin and Piperonyl Butoxide Topical"              
[3] "Antipyrine-Benzocaine Otic"  "Hydrocortisone  

So if i input Phenylephrine, vast should definetly be TRUE as it was before but now is returning only FALSE, is there any logical explanation to this?

camille
  • 16,432
  • 18
  • 38
  • 60
Mimic01
  • 107
  • 1
  • 12

1 Answers1

1

Try user_comp %in% medline_comps[[1]] (note the double brackets).

Your output indicates what's going on here:

medline_comps[1]
# [[1]] 
# [1] "Phenylephrine" "Pyrethrin and Piperonyl Butoxide Topical" 
# [3] "Antipyrine-Benzocaine Otic" "Hydrocortisone

The single bracket returns a list, whereas you wanted to test for the presence of an item in the vector contained in the list, in which case you would use the double brackets. See e.g. The difference between [] and [[]] notations for accessing the elements of a list or dataframe.

Weihuang Wong
  • 12,868
  • 2
  • 27
  • 48