我认为这可能是一个有趣的练习。
我将你的一些数据模拟成 csv。
> raw_input <- read.csv('date variable sample.csv')
> raw_input
Date Variable.1 Date2 Variable.2 Date4 Variable4
1 9/12/2009 82 9/12/2009 41 15/12/2009 0
2 9/12/2009 12:00 80 9/12/2009 12:00 38 23/03/2010 16:00 71
3 9/12/2009 16:00 80 9/12/2009 16:00 42 25/03/2010 21:00 73
然后我创建了一个 set.1, set.2 ... 将您的日期、变量对分隔到不同的数据帧中。
> set.1 <- raw_input[c('Date','Variable.1')]
> colnames(set.1) <- c('Date', 'Variable')
Date Variable
1 9/12/2009 82
2 9/12/2009 12:00 80
3 9/12/2009 16:00 80
> set.2 <- raw_input[c('Date2', 'Variable.2')]
> colnames(set.2) <- c('Date', 'Variable')
Date Variable
1 9/12/2009 41
2 9/12/2009 12:00 38
3 9/12/2009 16:00 42
> set.4 <- raw_input[c('Date4', 'Variable4')]
> colnames(set.4) <- c('Date', 'Variable')
Date Variable
1 15/12/2009 0
2 23/03/2010 16:00 71
3 25/03/2010 21:00 73
然后我用 Reduce() 合并所有数据帧。
> fin <- Reduce(function(x, y) merge(x, y, all=T, by=c("Date")), list(set.1, set.2, set.4))
> fin
Date Variable.x Variable.y Variable
1 9/12/2009 82 41 NA
2 9/12/2009 12:00 80 38 NA
3 9/12/2009 16:00 80 42 NA
4 15/12/2009 NA NA 0
5 23/03/2010 16:00 NA NA 71
6 25/03/2010 21:00 NA NA 73
您也可以标准化(忽略时间),这样您就可以对日期进行分组,但您可能不想这样做。
编码愉快!