如何使用data.frame()生成日期格式的x值和r格式的对应y值



我是r的绝对初学者。我的目标是生成一个数据表,其中x值为日期格式,y值为浮点格式。这些值是通过splinefun()插值生成的。生成样条函数后,我希望我的值显示在一个表中,这会导致我的代码出错。

library(ggplot2)
dat <- data.frame(x = as.Date(c("2000-01-01","2000-02-01","2000-03-01","2000-04-01"), format ="%Y-%m-%d"),
y = c(1000,1500,4000,2000))
f <- splinefun(dat$x, dat$y, method = "monoH.FC")
f <- data.frame(x = seq.Date(from = as.Date("2000-01-01"), to= as.Date("2000-04-01"), by = "day"), y = f(seq.Date(from = as.Date("2000-01-01"), to= as.Date("2000-04-01"), by = "day")))
f

data.frame(x=..., y =f(seq.Date(...))中y的格式似乎有问题。我得到错误

Error in Ops.Date(m[n], (x[iR] - x0[n])) : 
* not defined for "Date" objects

您可以使用y的值来执行splinefun的值,如下所示:

library(ggplot2)
dat <- data.frame(x = as.Date(c("2000-01-01","2000-02-01","2000-03-01","2000-04-01"), format ="%Y-%m-%d"),
y = c(1000,1500,4000,2000))
f <- splinefun(dat$x, dat$y, method = "monoH.FC")
f <- data.frame(x = seq.Date(from = as.Date("2000-01-01"), to= as.Date("2000-04-01"), by = "day"), 
y = f(dat$y))
f
#>             x         y
#> 1  2000-01-01 -159596.8
#> 2  2000-01-02 -151532.3
#> 3  2000-01-03 -111209.7
#> 4  2000-01-04 -143467.7
#> 5  2000-01-05 -159596.8
#> 6  2000-01-06 -151532.3
#> 7  2000-01-07 -111209.7
#> 8  2000-01-08 -143467.7
#> 9  2000-01-09 -159596.8
#> 10 2000-01-10 -151532.3
#> 11 2000-01-11 -111209.7
#> 12 2000-01-12 -143467.7
#> 13 2000-01-13 -159596.8
#> 14 2000-01-14 -151532.3
#> 15 2000-01-15 -111209.7
#> 16 2000-01-16 -143467.7
#> 17 2000-01-17 -159596.8
#> 18 2000-01-18 -151532.3
#> 19 2000-01-19 -111209.7
#> 20 2000-01-20 -143467.7
#> 21 2000-01-21 -159596.8
#> 22 2000-01-22 -151532.3
#> 23 2000-01-23 -111209.7
#> 24 2000-01-24 -143467.7
#> 25 2000-01-25 -159596.8
#> 26 2000-01-26 -151532.3
#> 27 2000-01-27 -111209.7
#> 28 2000-01-28 -143467.7
#> 29 2000-01-29 -159596.8
#> 30 2000-01-30 -151532.3
#> 31 2000-01-31 -111209.7
#> 32 2000-02-01 -143467.7
#> 33 2000-02-02 -159596.8
#> 34 2000-02-03 -151532.3
#> 35 2000-02-04 -111209.7
#> 36 2000-02-05 -143467.7
#> 37 2000-02-06 -159596.8
#> 38 2000-02-07 -151532.3
#> 39 2000-02-08 -111209.7
#> 40 2000-02-09 -143467.7
#> 41 2000-02-10 -159596.8
#> 42 2000-02-11 -151532.3
#> 43 2000-02-12 -111209.7
#> 44 2000-02-13 -143467.7
#> 45 2000-02-14 -159596.8
#> 46 2000-02-15 -151532.3
#> 47 2000-02-16 -111209.7
#> 48 2000-02-17 -143467.7
#> 49 2000-02-18 -159596.8
#> 50 2000-02-19 -151532.3
#> 51 2000-02-20 -111209.7
#> 52 2000-02-21 -143467.7
#> 53 2000-02-22 -159596.8
#> 54 2000-02-23 -151532.3
#> 55 2000-02-24 -111209.7
#> 56 2000-02-25 -143467.7
#> 57 2000-02-26 -159596.8
#> 58 2000-02-27 -151532.3
#> 59 2000-02-28 -111209.7
#> 60 2000-02-29 -143467.7
#> 61 2000-03-01 -159596.8
#> 62 2000-03-02 -151532.3
#> 63 2000-03-03 -111209.7
#> 64 2000-03-04 -143467.7
#> 65 2000-03-05 -159596.8
#> 66 2000-03-06 -151532.3
#> 67 2000-03-07 -111209.7
#> 68 2000-03-08 -143467.7
#> 69 2000-03-09 -159596.8
#> 70 2000-03-10 -151532.3
#> 71 2000-03-11 -111209.7
#> 72 2000-03-12 -143467.7
#> 73 2000-03-13 -159596.8
#> 74 2000-03-14 -151532.3
#> 75 2000-03-15 -111209.7
#> 76 2000-03-16 -143467.7
#> 77 2000-03-17 -159596.8
#> 78 2000-03-18 -151532.3
#> 79 2000-03-19 -111209.7
#> 80 2000-03-20 -143467.7
#> 81 2000-03-21 -159596.8
#> 82 2000-03-22 -151532.3
#> 83 2000-03-23 -111209.7
#> 84 2000-03-24 -143467.7
#> 85 2000-03-25 -159596.8
#> 86 2000-03-26 -151532.3
#> 87 2000-03-27 -111209.7
#> 88 2000-03-28 -143467.7
#> 89 2000-03-29 -159596.8
#> 90 2000-03-30 -151532.3
#> 91 2000-03-31 -111209.7
#> 92 2000-04-01 -143467.7

创建于2023-01-05 with reprex v2.0.2

相关内容

最新更新