代码之家  ›  专栏  ›  技术社区  ›  mr.T

如何旋转矢量/时间序列

  •  0
  • mr.T  · 技术社区  · 5 年前

    y

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

    我想把一个向量旋转任意度数。

    enter image description here

    我找到一个 similar question

    2 回复  |  直到 5 年前
        1
  •  2
  •   Waldi    5 年前

    你可以用 sf

    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)
    

        2
  •  1
  •   Nicolás Velasquez    5 年前

    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()
    

    enter image description here

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