我有一个动物园对象,我需要将动物园中的特定索引(#63)替换为新索引。
我当前的动物园索引是:
> index(MyZoo[63,])
[1] "2005-05-03 06:12:00 UTC"
我的新索引是:
> tmp_index
[1] "2005-05-03 06:29:00 UTC"
两者的结构相同:
> str(tmp_index)
POSIXct[1:1], format: "2005-05-03 06:29:00"
> str(index(MyZoo[63,]))
POSIXct[1:1], format: "2005-05-03 06:12:00"
我试试这段代码:
index(MyZoo[63,]) <- tmp_index
但结果相同且不起作用
> index(MyZoo[63,])
[1] "2005-05-03 06:12:00 UTC"
请帮帮我!!!如何将新索引替换为旧索引:)
答案 0 :(得分:1)
你可以尝试
index(MyZoo)[63] <- tmp_index
使用示例数据
index(MyZoo)[4] <- tmp_index
MyZoo
# V1 V2 V3
#2005-05-03 04:25:00 6 13 2
#2005-05-04 04:25:00 5 8 12
#2005-05-05 04:25:00 15 14 15
#2005-05-03 06:29:00 11 14 3 ###changed
#2005-05-07 04:25:00 14 7 5
#2005-05-08 04:25:00 19 19 14
#2005-05-09 04:25:00 6 4 2
#2005-05-10 04:25:00 16 1 12
#2005-05-11 04:25:00 17 11 13
#2005-05-12 04:25:00 6 3 1
tmp_index <- as.POSIXct('2005-05-03 06:29:00', format='%Y-%m-%d %H:%M:%S')
MyZoo <- structure(c(6L, 5L, 15L, 11L, 14L, 19L, 6L, 16L, 17L, 6L,
13L,
8L, 14L, 14L, 7L, 19L, 4L, 1L, 11L, 3L, 2L, 12L, 15L, 3L, 5L,
14L, 2L, 12L, 13L, 1L), .Dim = c(10L, 3L), .Dimnames = list(NULL,
c("V1", "V2", "V3")), index = structure(c(1115108700L, 1115195100L,
1115281500L, 1115367900L, 1115454300L, 1115540700L, 1115627100L,
1115713500L, 1115799900L, 1115886300L), class = c("POSIXct",
"POSIXt"), tzone = ""), class = "zoo")