0

我有两个数据框。数据框“天气”如下所示:

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

我尝试过合并和重塑2,但徒劳无功。我将非常感谢任何帮助。谢谢你。

4

1 回答 1

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。

于 2014-01-28T10:41:31.637 回答