[英]Why is dcast populating my (melted) dataframe with NAs?
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. 环顾四周并考虑要使用哪些工具,我认为我会使用reshape2软件包。
I started with myDF <- melt(myDF)
so I could melt my dataframe. 我从
myDF <- melt(myDF)
开始,所以我可以融合我的数据框。 The strategy is to use dcast
to reformat it as a long dataframe. 该策略是使用
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)
我尝试了
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? 有人可以帮我一下,告诉我为什么发生这种邪恶的事情吗?
You can use your original data frame inside function dcast()
because your data already are in long format. 您可以在函数
dcast()
使用原始数据帧,因为您的数据已经是长格式。 Function will use column Jobs
as values. 函数将使用
Jobs
列作为值。
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. 您也可以确切地写出您想使用
Jobs
列作为值。
dcast(df,Date~Company,value.var="Jobs")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.