Why is dcast populating my (melted) dataframe with NAs?

980 views Asked by At

I began with this innocuous dataframe:

Date          Company     Jobs   
1/1/2012      Company 1    12 
1/1/2012      Company 2    84
1/1/2012      Company 3    239
1/1/2012      Company 4    22

I am dreaming, begging, and fantasizing about this dataframe looking like this:

Date          Company 1   Company 2 Company 3 Company 4
1/1/2012         12          84       239        22
1/2/2012                
1/3/2012                     <other numbers here> 
1/4/2012      

Looking around and thinking about which tools to use, I figured I'd use the reshape2 package.
I started with myDF <- melt(myDF) so I could melt my dataframe. The strategy is to use dcast to reformat it as a long dataframe.

So here's my melted dataframe:

Date          Company     variable   value
1/1/2012      Company 1    Jobs       12 
1/1/2012      Company 2    Jobs       84
1/1/2012      Company 3    Jobs       239
1/1/2012      Company 4    Jobs       22

I tried dcast(myDF, Date ~ Company + value)
and got this:

Date          Company 1   Company 2 Company 3 Company 4
1/1/2012         NA          NA       NA        NA
1/2/2012                
1/3/2012                     <NAs here> 
1/4/2012      

Can someone please help me out and tell me why such a nefarious thing is occurring?

1

There are 1 answers

6
Didzis Elferts On

You can use your original data frame inside function dcast() because your data already are in long format. Function will use column Jobs as values.

dcast(df,Date~Company)
      Date Company_1 Company_2 Company_3 Company_4
1 1/1/2012        12        84       239        22

You can also write exactly that you want to use column Jobs as values.

dcast(df,Date~Company,value.var="Jobs")