【问题标题】:How to get a value of a column in a matrix based on the values in another matrix?如何根据另一个矩阵中的值获取矩阵中列的值?
【发布时间】:2017-12-18 19:37:30
【问题描述】:

数据

我有 2 个矩阵。 traf_id 表示两列中两个红绿灯的 ID(如果红绿灯离汽车最近,则位于第一列)。第二个矩阵traf_state 表示交通灯的状态(1 = 红色,2 = 绿色)(同样,最近的灯的状态放在第一列)。

traf_id <- matrix(data = c(rep(12353,4), rep(12453,4),  rep(12453,4), rep(12353,4)), nrow = 8, ncol = 2)
      [,1]  [,2]
[1,] 12353 12453
[2,] 12353 12453
[3,] 12353 12453
[4,] 12353 12453
[5,] 12453 12353
[6,] 12453 12353
[7,] 12453 12353
[8,] 12453 12353  


traf_state <- matrix(data = c(rep(1,8), rep(2,8)), nrow = 8, ncol = 2)
     [,1] [,2]
[1,]    1    2
[2,]    1    2
[3,]    1    2
[4,]    1    2
[5,]    1    2
[6,]    1    2
[7,]    1    2
[8,]    1    2  

在上述数据中,交通信号灯12353 在前 4 个时间帧(traf_state 等于 1)中距离汽车最近,因此放在第一列。从第 5 帧到第 8 帧,12453 是最接近的。

我想做什么

我想创建一个包含 3 列的数据框。第一个应该是时间范围列,另外两个应该包含给定时间范围内给定交通灯的交通状态:

foo <- data.frame(frames = 1:8, state_12353 = c(rep(1,4), rep(2,4)), state_12453 = c(rep(2,4), rep(1,4)))
> foo
  frames state_12353 state_12453
1      1           1           2
2      2           1           2
3      3           1           2
4      4           1           2
5      5           2           1
6      6           2           1
7      7           2           1
8      8           2           1 

请指导我dplyr 中的哪些函数与创建上述数据框相关。

【问题讨论】:

    标签: r matrix dplyr


    【解决方案1】:

    您似乎有代表时间范围的行,以及代表交通灯与汽车的相对位置的列;要将交通灯 ID 与其状态相匹配,您需要将原始数据收集为长格式,以便坐标(时间和位置)成为两个单独的列,然后您可以加入:

    # make the data frames
    traf_id_df <- data.frame(traf_id, frames = 1:8)
    traf_state_df <- data.frame(traf_state, frames = 1:8)
    
    library(dplyr); library(tidyr)
    inner_join(
        gather(traf_id_df, Distance, Id, -frames), 
        gather(traf_state_df, Distance, State, -frames)
    ) %>% 
        select(-Distance) %>% 
        mutate(Id = paste0("state_", Id)) %>% 
        spread(Id, State)
    
    #  frames state_12353 state_12453
    #1      1           1           2
    #2      2           1           2
    #3      3           1           2
    #4      4           1           2
    #5      5           2           1
    #6      6           2           1
    #7      7           2           1
    #8      8           2           1
    

    或者由于两个矩阵中的元素相互对应,你可以将两个矩阵逐元素绑定,然后reshape:

    cbind(
        seq_len(nrow(traf_id)), 
        as.vector(traf_id), 
        as.vector(traf_state)
    ) %>% as.data.frame() %>% 
        setNames(c('frames', 'id', 'state')) %>% 
        mutate(id = paste0('state_', id)) %>% 
        spread(id, state)
    
    #  frames state_12353 state_12453
    #1      1           1           2
    #2      2           1           2
    #3      3           1           2
    #4      4           1           2
    #5      5           2           1
    #6      6           2           1
    #7      7           2           1
    #8      8           2           1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多