R: approxfun 抱怨 "object cannot be coerced to type 'double'"
R: approxfun complains about "object cannot be coerced to type 'double'"
我有一个类似于(甚至更简单)的解决方案:
但这对我不起作用。
我已经使用 dput 对我的数据进行了快照。
a <- structure(list(
time = structure(
c(1566381574.097, 1566381542.104, 1566381510.109, 1566381390.134, 1566381330.118,
1566381300.107, 1566381240.114, 1566381210.114, 1566381173.903, 1566381148.113
), class = c("POSIXct", "POSIXt"), tzone = ""),
value = c(164.1, 162.3, 161.1, 158.6, 159.6, 157.6, 159, 157.8, 155.3, 155.3
)), class = "data.frame", row.names = c(NA, 10L))
b <- structure(list(time = structure(
c(1566381120, 1566381180, 1566381240, 1566381300, 1566381360, 1566381420, 1566381480, 1566381540
), class = c("POSIXct", "POSIXt"), tzone = "")), row.names = c(NA, 8L), class = "data.frame")
head(a)
head(b)
result <- b
result$value <- approxfun(a$time,a$value)(b)
这是我得到的:
> head(a)
time value
1 2019-08-21 11:59:34 164.1
2 2019-08-21 11:59:02 162.3
3 2019-08-21 11:58:30 161.1
4 2019-08-21 11:56:30 158.6
5 2019-08-21 11:55:30 159.6
6 2019-08-21 11:55:00 157.6
> head(b)
time
1 2019-08-21 11:52:00
2 2019-08-21 11:53:00
3 2019-08-21 11:54:00
4 2019-08-21 11:55:00
5 2019-08-21 11:56:00
6 2019-08-21 11:57:00
>
> result <- b
> result$value <- approxfun(a$time,a$value)(b)
Error in .approxfun(x, y, v, method, yleft, yright, f) :
(list) object cannot be coerced to type 'double'
根据?approxfun
The function approxfun returns a function performing (linear or constant) interpolation of the given data points. For a given set of x values, this function will return the corresponding interpolated values.
Here, the 'b' is a data.frame, may be we need
x, y - numeric vectors giving the coordinates of the points to be interpolated.
根据描述,由于 approxfun
正在返回一个函数,它根据一组 x
(numeric vector
) 进行插值,传递一个 data.frame
将不行。因此,提取特定列作为插值向量
approxfun(a$time,a$value)(b$time)
我有一个类似于(甚至更简单)的解决方案:
但这对我不起作用。 我已经使用 dput 对我的数据进行了快照。
a <- structure(list(
time = structure(
c(1566381574.097, 1566381542.104, 1566381510.109, 1566381390.134, 1566381330.118,
1566381300.107, 1566381240.114, 1566381210.114, 1566381173.903, 1566381148.113
), class = c("POSIXct", "POSIXt"), tzone = ""),
value = c(164.1, 162.3, 161.1, 158.6, 159.6, 157.6, 159, 157.8, 155.3, 155.3
)), class = "data.frame", row.names = c(NA, 10L))
b <- structure(list(time = structure(
c(1566381120, 1566381180, 1566381240, 1566381300, 1566381360, 1566381420, 1566381480, 1566381540
), class = c("POSIXct", "POSIXt"), tzone = "")), row.names = c(NA, 8L), class = "data.frame")
head(a)
head(b)
result <- b
result$value <- approxfun(a$time,a$value)(b)
这是我得到的:
> head(a)
time value
1 2019-08-21 11:59:34 164.1
2 2019-08-21 11:59:02 162.3
3 2019-08-21 11:58:30 161.1
4 2019-08-21 11:56:30 158.6
5 2019-08-21 11:55:30 159.6
6 2019-08-21 11:55:00 157.6
> head(b)
time
1 2019-08-21 11:52:00
2 2019-08-21 11:53:00
3 2019-08-21 11:54:00
4 2019-08-21 11:55:00
5 2019-08-21 11:56:00
6 2019-08-21 11:57:00
>
> result <- b
> result$value <- approxfun(a$time,a$value)(b)
Error in .approxfun(x, y, v, method, yleft, yright, f) :
(list) object cannot be coerced to type 'double'
根据?approxfun
The function approxfun returns a function performing (linear or constant) interpolation of the given data points. For a given set of x values, this function will return the corresponding interpolated values. Here, the 'b' is a data.frame, may be we need
x, y - numeric vectors giving the coordinates of the points to be interpolated.
根据描述,由于 approxfun
正在返回一个函数,它根据一组 x
(numeric vector
) 进行插值,传递一个 data.frame
将不行。因此,提取特定列作为插值向量
approxfun(a$time,a$value)(b$time)