0

I have two data frames containing information from various hospitals. The first has number of probable cases of dengue and the second has number of comfirmed cases of dengues.The data is given weekly wise. I have data upto 53 weeks or 1 year. Example-

    Data Frame 1(Probable cases)
    HospitalName Week1 Week 2
         xyz       8     12
         abc       9      0
    Data Frame 2(Laboratory verified cases)
    HospitalName Week1 Week 2
         xyz       3     11
         abc      14      0

Both the data frames have the same names of hospitals in corresponding rows. I want to find the similarity between the two data frames using cosine similarity in R. How to do it?

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
amankedia
  • 377
  • 2
  • 8
  • 23
  • Anything you already tried? Why did it not work? – Heroka Jan 07 '16 at 06:50
  • 1
    `install.packages("sos", dep = TRUE); library(sos); findFn("cosine similarity")`. Good search. –  Jan 07 '16 at 06:50
  • @Pascal does that function help to find cosine similarity among the two data frames? If yes, can you please tell me how do I pass the data frames to the function? – amankedia Jan 07 '16 at 07:32
  • No. It searches for you a (possibly) suitable function to your problem. Nothing else. –  Jan 07 '16 at 07:34

1 Answers1

0
install.packages("proxy")
dist(x,y,method = "cosine")

#       [,1]       [,2]      
#[1,] 0.03909305 0.44529980
#[2,] 0.23661371 0.00000000

x and y are Week1, Week2 from df1 and Week1, Week2 from df2

x <- as.matrix(Data Frame 1[,2:3])
y <- as.matrix(Data Frame 2[,2:3])
Sotos
  • 51,121
  • 6
  • 32
  • 66
  • I tried the dist function but got the following error: dist(combined[,1],laboratory[,1],method="cosine") Error in dist(combined[, 1], laboratory[, 1], method = "cosine") : Can only handle data frames, vectors, matrices, and lists! – amankedia Jan 07 '16 at 08:17
  • Yes. So convert them to matrices, or df...I converted Weeks 1&2 from both dataframes into matrices x and y – Sotos Jan 07 '16 at 08:21