这是一个非常基本的reshape
问题。假设你data.frame
被称为“mydf”:
> reshape(mydf, direction = "wide", idvar="DATE", timevar="SERVER")
DATE pc.server1 pc.server2
1 2013-02-16 01:00:00 3.83 4.74
2 2013-02-16 02:00:00 3.45 5.70
3 2013-02-16 03:00:00 3.34 8.54
4 2013-02-16 04:00:00 3.73 9.25
5 2013-02-16 05:00:00 3.16 10.12
6 2013-02-16 06:00:00 3.16 10.15
或者,使用“reshape2”包:
> library(reshape2)
> dcast(mydf, DATE ~ SERVER, value.var="pc")
DATE server1 server2
1 2013-02-16 01:00:00 3.83 4.74
2 2013-02-16 02:00:00 3.45 5.70
3 2013-02-16 03:00:00 3.34 8.54
4 2013-02-16 04:00:00 3.73 9.25
5 2013-02-16 05:00:00 3.16 10.12
6 2013-02-16 06:00:00 3.16 10.15
如果您有重复的“DATE”和“SERVER”组合,那么您需要在数据中添加一个辅助“ID”变量。
以下是一些示例数据(请在以后以这种形式分享您的数据):
mydf <- structure(list(DATE = c("2013-02-16 01:00:00", "2013-02-16 02:00:00",
"2013-02-16 03:00:00", "2013-02-16 04:00:00", "2013-02-16 05:00:00",
"2013-02-16 06:00:00", "2013-02-16 01:00:00", "2013-02-16 02:00:00",
"2013-02-16 03:00:00", "2013-02-16 04:00:00", "2013-02-16 05:00:00",
"2013-02-16 06:00:00", "2013-02-16 01:00:00"), pc = c(3.83, 3.45,
3.34, 3.73, 3.16, 3.16, 4.74, 5.7, 8.54, 9.25, 10.12, 10.15,
5.83), SERVER = c("server1", "server1", "server1", "server1",
"server1", "server1", "server2", "server2", "server2", "server2",
"server2", "server2", "server1")), .Names = c("DATE", "pc", "SERVER"
), row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12", "13"), class = "data.frame")
mydf
# DATE pc SERVER
# 1 2013-02-16 01:00:00 3.83 server1
# 2 2013-02-16 02:00:00 3.45 server1
# 3 2013-02-16 03:00:00 3.34 server1
# 4 2013-02-16 04:00:00 3.73 server1
# 5 2013-02-16 05:00:00 3.16 server1
# 6 2013-02-16 06:00:00 3.16 server1
# 7 2013-02-16 01:00:00 4.74 server2
# 8 2013-02-16 02:00:00 5.70 server2
# 9 2013-02-16 03:00:00 8.54 server2
# 10 2013-02-16 04:00:00 9.25 server2
# 11 2013-02-16 05:00:00 10.12 server2
# 12 2013-02-16 06:00:00 10.15 server2
# 13 2013-02-16 01:00:00 5.83 server1
请注意,由于第 1 行和第 13 行中重复的“DATE”+“SERVER”组合,我们将无法在reshape
没有收到您提到的警告的情况下使用。解决方案:添加辅助 ID:
mydf$ID <- ave(as.character(mydf$DATE), mydf$DATE, mydf$SERVER, FUN = seq_along)
reshape(mydf, direction = "wide", idvar=c("DATE", "ID"), timevar="SERVER")
# DATE ID pc.server1 pc.server2
# 1 2013-02-16 01:00:00 1 3.83 4.74
# 2 2013-02-16 02:00:00 1 3.45 5.70
# 3 2013-02-16 03:00:00 1 3.34 8.54
# 4 2013-02-16 04:00:00 1 3.73 9.25
# 5 2013-02-16 05:00:00 1 3.16 10.12
# 6 2013-02-16 06:00:00 1 3.16 10.15
# 13 2013-02-16 01:00:00 2 5.83 NA