1

I have data in excel as:

     Terms       Category  Weight

      email       TV          1.00

      acccount    Email       12.0

      accept      Phone       3.00

I have other matrix and its format is:

   Terms   TV    Email  Phone  Contact    Information  Support .....

   achieve  1    0.       0      0         0             0
   acquired 0    10.20    0      0         0             0
   across   0    0        3.00   0          0            0

now I want to convert above data into above format like

   Terms   TV    Email  Phone  Contact    Information  Support .....

    email    1    0.       0      0         0.0          0
    acccount 0    12.0    0      0         0.0          0
    accept   0    0        0      1.23       0          0

I want to do this by program in R. Any help will be appreciated. Thanks in advance.

indra_patil
  • 283
  • 1
  • 4
  • 11

1 Answers1

3

You need to reshape your data. Install the package "reshape2" if you don't already have it

Here's the code to reshape your data

require(reshape2)
df.reshape <-melt(df, id.var=c("Terms", "Category")) 
#where df is your data.frame to be reshaped
#using both terms and category as ID variables
#now reshape it to wide format by casting
df.wide <-dcast(df.reshape, Terms~Category)

Note this will give you NA's for pairs that don't exist in your data. Which you can easily replace with zeros if you want

And here's a nice tutorial for using reshape2 http://www.seananderson.ca/2013/10/19/reshape.html

infominer
  • 1,981
  • 13
  • 17