如何旋转向量/时间序列

how to rotate vector / time series

例如我有这样一个向量y

y <- c(1,7,4,8,4,6,1)
x <- c(1,2,3,4,5,6,7) # indexes

我想将矢量旋转任意度数。

输入向量 --> 旋转函数 --> 旋转向量。

像这样

我找到了一个similar question,但是我不擅长公式,谁能用代码举个例子

你可以使用 sf:

  • 创建一条曲线 linestring
  • 创建 rotation matrix
  • 将曲线乘以旋转矩阵
library(sf)

x <- c(1,7,4,8,4,6,1)
curve <- st_linestring(cbind(1:length(x), x))  


theta <- pi / 3
rotation <- matrix(c(cos(theta),sin(theta),-sin(theta),cos(theta)),ncol =2)
plot(curve)

plot(curve*rotation)

使用 {tidyverse} 和 {spdep}。

library(tidyverse)
library(spdep)

y <- c(1,7,4,8,4,6,1)
x <- c(1,2,3,4,5,6,7)

base <- data.frame(x,y)

rotated <- 
  data.frame(x = x, y = y) %>% 
  spdep::Rotation(angle = 0.5) %>% 
  data.frame() %>% rename(x = 1, y = 2)

bind_rows(base %>% mutate(id = "base"),
          rotated %>% mutate(id = "rotated")) %>% 
  ggplot(aes(x = x, y = y, colour = id)) +
    geom_path()

由 reprex 包 (v2.0.0) 创建于 2021-04-23