0

I am trying to build a item frequency matrix in R with the following data.

H_USERID    H_AUDIOID
1   TRAAAAW128F429D538.h5
2   TRAAABD128F429CF47.h5
2   TRAAADZ128F9348C2E.h5
3   TRAAAEF128F4273421.h5
3   TRAAAFD128F92F423A.h5
3   TRAAAMO128F1481E7F.h5
3   TRAAAMQ128F1460CD3.h5
3   TRAAAPK128E0786D96.h5
3   TRAAARJ128F9320760.h5
4   TRAAAVG12903CFA543.h5
4   TRAAAVO128F93133D4.h5
4   TRAABCL128F4286650.h5
4   TRAABDL12903CAABBA.h5

All I want is a table with the unique users as rows and audioID as columns. I understand that I need to use dcast method to do this, but cant figure out exactly how to do. I tried the following code, but i know its not the proper solution.

user = read.csv("temp.csv")
step1 = melt(user)
output <- dcast(step1, user$H_AUDIOID~value, fun.aggregate = length)
output = t(output)

The desired output is :

user$H_AUDIOID  TRAAAAW128F429D538.h5   TRAAABD128F429CF47.h5   TRAAADZ128F9348C2E.h5   TRAAAEF128F4273421.h5   TRAAAFD128F92F423A.h5   TRAAAMO128F1481E7F.h5   TRAAAMQ128F1460CD3.h5   TRAAAPK128E0786D96.h5   TRAAARJ128F9320760.h5   TRAAAVG12903CFA543.h5   TRAAAVO128F93133D4.h5   TRAABCL128F4286650.h5   TRAABDL12903CAABBA.h5
1   1   0   0   0   0   0   0   0   0   0   0   0   0
2   0   1   1   0   0   0   0   0   0   0   0   0   0
3   0   0   0   1   1   1   1   1   1   0   0   0   0
4   0   0   0   0   0   0   0   0   0   1   1   1   1

Any help will be useful.

scooby
  • 493
  • 11
  • 31

0 Answers0