我有两个数据框。数据框“天气”如下所示:
weather<-data.frame(Date=c("2012-04-01","2012-04-02","2012-04-03","2012-04-04"),Day=c("Sunday","Monday","Tuesday","Wednesday"), Temp=c(86,89,81,80))
Date Day Temperature
2012-04-01 Sunday 86
2012-04-02 Monday 89
2012-04-03 Tuesday 81
2012-04-04 Wednesday 80
而且,数据框“Regularity”,如下所示:
Regularity<-data.frame(Date=c("2012-04-02","2012-04-04","2012-04-03","2012-04-04"),EmployeeID=c(1,1,2,2),Attendance=c(1,1,1,1))
Date EmployeeID Attendance
2012-04-02 1 1
2012-04-04 1 1
2012-04-03 2 1
2012-04-04 2 1
我想在R中创建一个面板数据框:
Date Day Temperature EmployeeID Attendence
2012-04-01 Sunday 86 1 0
2012-04-02 Monday 89 1 1
2012-04-03 Tuesday 81 1 0
2012-04-04 Wednesday 80 1 1
2012-04-01 Sunday 86 2 0
2012-04-02 Monday 89 2 0
2012-04-03 Tuesday 81 2 1
2012-04-04 Wednesday 80 2 1
我尝试过合并和reshape2,但是徒劳无功。我将非常感谢任何帮助。谢谢。
答案 0 :(得分:1)
这是怎么回事。假设tb1
是第一个表,tb2
是第二个表。然后通过以下方式实现所需的结果:
tb2_tf<-dcast(tb2,Date~EmployeeID,value.var="Attendance")
tb<-melt(merge(tb1,tb2_tf,all=TRUE),id=1:3,variable.name="EmployeeID",value.name="Attendance")
tb$Attendance[is.na(tb$Attendance)] <- 0
tb
Date Day Temperature EmployeeID Attendance
1 2012-04-01 Sunday 86 1 0
2 2012-04-02 Monday 89 1 1
3 2012-04-03 Tuesday 81 1 0
4 2012-04-04 Wednesday 80 1 1
5 2012-04-01 Sunday 86 2 0
6 2012-04-02 Monday 89 2 0
7 2012-04-03 Tuesday 81 2 1
8 2012-04-04 Wednesday 80 2 1
我希望看到没有重塑部分的解决方案。我怀疑有人使用某种形式的theta join。