为什么dcast用NAs填充我的(融化的)数据帧?

时间:2021-10-19 16:17:28

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.

环顾四周并考虑使用哪些工具,我想我会使用reshape2包。我开始使用myDF < - melt(myDF),所以我可以融化我的数据帧。策略是使用dcast将其重新格式化为长数据帧。

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:

我试过dcast(myDF,Date~Company + value)得到了这个:

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 个解决方案

#1


1  

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()中使用原始数据框,因为您的数据已经是长格式。函数将使用列作为值。

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")

#1


1  

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()中使用原始数据框,因为您的数据已经是长格式。函数将使用列作为值。

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")