我有两个数据框。数据帧“天气”看起来像这样:
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 Temp
2012-04-01 Sunday 86
2012-04-02 Monday 89
2012-04-03 Tuesday 81
2012-04-04 Wednesday 80
而且,dataframe“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 :(得分:0)
您可以使用双merge
和expand.grid
,如下所示:
merge(weather,
merge(Regularity,
expand.grid(Date = unique(weather$Date),
EmployeeID = unique(Regularity$EmployeeID)),
all = TRUE))
# Date Day Temp EmployeeID Attendance
# 1 2012-04-01 Sunday 86 1 NA
# 2 2012-04-01 Sunday 86 2 NA
# 3 2012-04-02 Monday 89 1 1
# 4 2012-04-02 Monday 89 2 NA
# 5 2012-04-03 Tuesday 81 1 NA
# 6 2012-04-03 Tuesday 81 2 1
# 7 2012-04-04 Wednesday 80 1 1
# 8 2012-04-04 Wednesday 80 2 1
expand.grid
步骤是获取&#34;日期&#34;的所有组合。和&#34;员工ID&#34;这是完整数据集所必需的。
您可以使用&#34; 0&#34;轻松替换NA
值在另一个步骤。