我有一个像这样的数据框:
date_time id Temp
2012-02-27 00:12:30 | Sens_1 | 25.2
2012-02-27 00:13:00 | Sens_1 | 25.5
2012-02-27 00:13:30 | Sens_1 | 26.3
2012-02-27 00:14:00 | Sens_1 | 26.3
2012-02-27 00:14:30 | Sens_1 | 26.3
: : :
2012-02-27 00:12:30 | Sens_2 | 35.4
2012-02-27 00:13:00 | Sens_2 | 36.5
2012-02-27 00:13:30 | Sens_2 | 38.8
2012-02-27 00:14:30 | Sens_2 | 36.3
: : :
2012-02-27 00:12:30 | Sens_3 | 15.4
2012-02-27 00:13:00 | Sens_3 | 15.5
2012-02-27 00:13:30 | Sens_3 | 16.8
2012-02-27 00:14:30 | Sens_3 | 16.3
: : :
我希望得到另一个看起来像这样的数据框:
date_time Sens_1 | Sens_2 | Sens_3
2012-02-27 00:12:30 | 25.2 | 35.4 | 15.4
2012-02-27 00:13:00 | 25.5 | 36.5 | 15.5
2012-02-27 00:13:30 | 26.3 | 38.8 | 16.8
2012-02-27 00:14:00 | 26.3 | NA | NA
2012-02-27 00:14:30 | 26.3 | 36.3 | 16.3
: : : :
我怎样才能实现这一目标?尽管如此,我在上面提到的所有三个变量都是相同的" date_time"值(因此在大多数情况下)存在某些行,其中任何变量子组都可以具有某个" date_time"价值,其余的赢了。
答案 0 :(得分:0)
以下是单行:
library( 'data.table' )
setDT( df1 ) # convert data frame to data table by reference
dcast( df1, date_time ~ id, value.var = 'Temp', fun = function( x ) x, fill = NA_real_ )
# date_time Sens_1 Sens_2 Sens_3
# 1: 2012-02-27 00:11:00 23.0 34.6 14.6
# 2: 2012-02-27 00:11:30 23.4 33.4 14.4
# 3: 2012-02-27 00:12:00 34.6 NA 13.6
# 4: 2012-02-27 00:12:30 35.4 NA 15.4
# 5: 2012-02-27 00:13:00 25.5 36.5 15.5
# 6: 2012-02-27 00:13:30 26.3 38.8 16.8
数据:强>
df1 <- structure(list(date_time = c("2012-02-27 00:11:00", "2012-02-27 00:11:30",
"2012-02-27 00:12:00", "2012-02-27 00:12:30", "2012-02-27 00:13:00",
"2012-02-27 00:13:30", "2012-02-27 00:11:00", "2012-02-27 00:11:30",
"2012-02-27 00:12:00", "2012-02-27 00:12:30", "2012-02-27 00:13:00",
"2012-02-27 00:13:30", "2012-02-27 00:11:00", "2012-02-27 00:11:30",
"2012-02-27 00:12:00", "2012-02-27 00:12:30", "2012-02-27 00:13:00",
"2012-02-27 00:13:30"),
id = c("Sens_1", "Sens_1", "Sens_1", "Sens_1", "Sens_1", "Sens_1", "Sens_2", "Sens_2",
"Sens_1", "Sens_1", "Sens_2", "Sens_2", "Sens_3", "Sens_3", "Sens_3", "Sens_3",
"Sens_3", "Sens_3"),
Temp = c(23, 23.4, 24.6, 25.2, 25.5, 26.3, 34.6, 33.4, 34.6, 35.4, 36.5, 38.8, 14.6,
14.4, 13.6, 15.4, 15.5, 16.8)), .Names = c("date_time", "id", "Temp"),
row.names = c(NA, -18L), class = "data.frame")