如何基于列合并和绘制2个数据的值

时间:2019-05-16 10:02:10

标签: r dataframe matrix ggplot2

我正在处理时间使用数据,我想确定人们在家时的活动时间。

我有两个数据框-whereHome; 1代表在家,0代表不在家

    dput(head(whereHome, 4))
    structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 
    1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 
    0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 
    1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 
    1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 
    1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 
    0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 
    0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 
    1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 
    0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), .Dim = c(4L, 144L), .Dimnames = list(
        NULL, c("04:00", "04:10", "04:20", "04:30", "04:40", "04:50", 
        "05:00", "05:10", "05:20", "05:30", "05:40", "05:50", "06:00", 
        "06:10", "06:20", "06:30", "06:40", "06:50", "07:00", "07:10", 
        "07:20", "07:30", "07:40", "07:50", "08:00", "08:10", "08:20", 
        "08:30", "08:40", "08:50", "09:00", "09:10", "09:20", "09:30", 
        "09:40", "09:50", "10:00", "10:10", "10:20", "10:30", "10:40", 
        "10:50", "11:00", "11:10", "11:20", "11:30", "11:40", "11:50", 
        "12:00", "12:10", "12:20", "12:30", "12:40", "12:50", "13:00", 
        "13:10", "13:20", "13:30", "13:40", "13:50", "14:00", "14:10", 
        "14:20", "14:30", "14:40", "14:50", "15:00", "15:10", "15:20", 
        "15:30", "15:40", "15:50", "16:00", "16:10", "16:20", "16:30", 
        "16:40", "16:50", "17:00", "17:10", "17:20", "17:30", "17:40", 
        "17:50", "18:00", "18:10", "18:20", "18:30", "18:40", "18:50", 
        "19:00", "19:10", "19:20", "19:30", "19:40", "19:50", "20:00", 
        "20:10", "20:20", "20:30", "20:40", "20:50", "21:00", "21:10", 
        "21:20", "21:30", "21:40", "21:50", "22:00", "22:10", "22:20", 
        "22:30", "22:40", "22:50", "23:00", "23:10", "23:20", "23:30", 
        "23:40", "23:50", "00:00", "00:10", "00:20", "00:30", "00:40", 
        "00:50", "01:00", "01:10", "01:20", "01:30", "01:40", "01:50", 
        "02:00", "02:10", "02:20", "02:30", "02:40", "02:50", "03:00", 
        "03:10", "03:20", "03:30", "03:40", "03:50")))
  

和具有以下结构的AggregatedAgr-其中11,82,41,   等表示活动

structure(list(`04:00` = c(11, 11, 11, 11), `04:10` = c(11, 11, 
11, 11), `04:20` = c(11, 11, 11, 11), `04:30` = c(11, 11, 11, 
11), `04:40` = c(11, 11, 11, 11), `04:50` = c(11, 11, 11, 11), 
    `05:00` = c(11, 11, 11, 11), `05:10` = c(11, 11, 11, 11), 
    `05:20` = c(11, 11, 11, 11), `05:30` = c(11, 11, 11, 11), 
    `05:40` = c(11, 11, 11, 11), `05:50` = c(92, 11, 11, 11), 
    `06:00` = c(13, 11, 11, 11), `06:10` = c(42, 11, 11, 11), 
    `06:20` = c(41, 11, 11, 11), `06:30` = c(82, 11, 11, 11), 
    `06:40` = c(12, 13, 11, 11), `06:50` = c(48, 41, 11, 11), 
    `07:00` = c(12, 43, 11, 11), `07:10` = c(12, 43, 13, 11), 
    `07:20` = c(12, 12, 12, 11), `07:30` = c(43, 12, 13, 11), 
    `07:40` = c(42, 12, 13, 11), `07:50` = c(42, 12, 200, 11), 
    `08:00` = c(42, 43, 200, 11), `08:10` = c(42, 43, 200, 11
    ), `08:20` = c(41, 82, 200, 11), `08:30` = c(41, 82, 200, 
    11), `08:40` = c(41, 82, 200, 11), `08:50` = c(41, 82, 200, 
    11), `09:00` = c(82, 44, 31, 11), `09:10` = c(61, 44, 31, 
    11), `09:20` = c(61, 42, 31, 11), `09:30` = c(61, 42, 31, 
    41), `09:40` = c(61, 42, 31, 83), `09:50` = c(61, 42, 31, 
    83), `10:00` = c(82, 12, 31, 83), `10:10` = c(82, 61, 31, 
    83), `10:20` = c(82, 42, 31, 83), `10:30` = c(82, 42, 31, 
    83), `10:40` = c(82, 61, 31, 83), `10:50` = c(82, 61, 31, 
    83), `11:00` = c(41, 61, 31, 92), `11:10` = c(61, 61, 31, 
    92), `11:20` = c(61, 61, 31, 92), `11:30` = c(91, 61, 31, 
    92), `11:40` = c(91, 61, 31, 92), `11:50` = c(91, 200, 31, 
    92), `12:00` = c(91, 46, 31, 92), `12:10` = c(91, 46, 31, 
    92), `12:20` = c(91, 46, 31, 92), `12:30` = c(91, 46, 31, 
    92), `12:40` = c(91, 46, 31, 92), `12:50` = c(91, 46, 31, 
    92), `13:00` = c(12, 46, 31, 46), `13:10` = c(12, 12, 31, 
    46), `13:20` = c(12, 12, 12, 46), `13:30` = c(12, 12, 12, 
    46), `13:40` = c(12, 12, 12, 46), `13:50` = c(12, 12, 12, 
    46), `14:00` = c(200, 46, 12, 46), `14:10` = c(200, 46, 12, 
    46), `14:20` = c(200, 200, 12, 46), `14:30` = c(200, 200, 
    31, 46), `14:40` = c(200, 42, 31, 46), `14:50` = c(200, 42, 
    31, 46), `15:00` = c(46, 42, 31, 46), `15:10` = c(46, 41, 
    31, 46), `15:20` = c(46, 41, 31, 46), `15:30` = c(46, 41, 
    200, 46), `15:40` = c(46, 41, 200, 46), `15:50` = c(46, 41, 
    200, 46), `16:00` = c(42, 41, 200, 46), `16:10` = c(42, 41, 
    200, 46), `16:20` = c(41, 41, 200, 46), `16:30` = c(41, 41, 
    200, 42), `16:40` = c(41, 41, 200, 42), `16:50` = c(41, 41, 
    200, 42), `17:00` = c(82, 41, 83, 83), `17:10` = c(82, 63, 
    83, 83), `17:20` = c(82, 63, 83, 83), `17:30` = c(82, 63, 
    83, 83), `17:40` = c(82, 63, 83, 83), `17:50` = c(82, 63, 
    83, 83), `18:00` = c(12, 41, 83, 83), `18:10` = c(12, 41, 
    83, 83), `18:20` = c(12, 12, 31, 83), `18:30` = c(12, 12, 
    31, 83), `18:40` = c(12, 12, 31, 31), `18:50` = c(92, 12, 
    31, 31), `19:00` = c(92, 42, 12, 13), `19:10` = c(92, 42, 
    12, 92), `19:20` = c(92, 42, 12, 92), `19:30` = c(92, 42, 
    92, 92), `19:40` = c(92, 42, 92, 92), `19:50` = c(92, 42, 
    92, 92), `20:00` = c(41, 41, 92, 92), `20:10` = c(41, 41, 
    92, 92), `20:20` = c(41, 41, 92, 92), `20:30` = c(41, 92, 
    41, 92), `20:40` = c(41, 92, 12, 92), `20:50` = c(61, 92, 
    31, 92), `21:00` = c(82, 92, 31, 92), `21:10` = c(82, 92, 
    31, 61), `21:20` = c(82, 92, 31, 61), `21:30` = c(82, 92, 
    13, 61), `21:40` = c(82, 92, 92, 61), `21:50` = c(82, 92, 
    92, 61), `22:00` = c(61, 13, 13, 11), `22:10` = c(61, 13, 
    11, 11), `22:20` = c(61, 13, 11, 11), `22:30` = c(61, 13, 
    11, 11), `22:40` = c(61, 13, 11, 11), `22:50` = c(61, 13, 
    11, 11), `23:00` = c(61, 11, 11, 11), `23:10` = c(61, 11, 
    11, 11), `23:20` = c(61, 11, 11, 11), `23:30` = c(61, 11, 
    11, 11), `23:40` = c(82, 11, 11, 11), `23:50` = c(82, 11, 
    11, 11), `00:00` = c(82, 11, 11, 11), `00:10` = c(82, 11, 
    11, 11), `00:20` = c(82, 11, 11, 11), `00:30` = c(82, 11, 
    11, 11), `00:40` = c(82, 11, 11, 11), `00:50` = c(82, 11, 
    11, 11), `01:00` = c(11, 11, 11, 11), `01:10` = c(11, 11, 
    11, 11), `01:20` = c(11, 11, 11, 11), `01:30` = c(11, 11, 
    11, 11), `01:40` = c(11, 11, 11, 11), `01:50` = c(11, 11, 
    11, 11), `02:00` = c(11, 11, 11, 11), `02:10` = c(11, 11, 
    11, 11), `02:20` = c(11, 11, 11, 11), `02:30` = c(11, 11, 
    11, 11), `02:40` = c(11, 11, 11, 11), `02:50` = c(11, 11, 
    11, 11), `03:00` = c(11, 11, 11, 11), `03:10` = c(11, 11, 
    11, 11), `03:20` = c(11, 11, 11, 11), `03:30` = c(11, 11, 
    11, 11), `03:40` = c(11, 11, 11, 11), `03:50` = c(11, 11, 
    11, 11)), row.names = c(NA, 4L), class = "data.frame")

两个数据帧中常见的是时间步长-在两个数据帧中,时间始于04:00,结束于:03.45。

我试图根据时间融化数据帧并将它们连接起来,但仍然无法正常工作。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我希望您能适应以下管道:

library(tidyverse)

left_join(

  t(whereHome) %>%
    data.frame() %>%
    rownames_to_column('time') %>%
    gather('id', 'home', -time),

  t(AggregatedAgr) %>%
    data.frame() %>%
    rownames_to_column('time') %>%
    gather('id', 'activity', -time),

  by = c('time', 'id')

  ) %>%
  mutate(
    id   = factor(paste0('Something #', gsub('[[:alpha:]]+', '', id))),
    home = factor(home, labels = c("Outside", "At home")),
    time = lubridate::hm(time)
  )

#       time           id    home activity
# 1 4H  0M 0S Something #1 At home       11
# 2 4H 10M 0S Something #1 At home       11
# 3 4H 20M 0S Something #1 At home       11
# 4 4H 30M 0S Something #1 At home       11
# 5 4H 40M 0S Something #1 At home       11
# 6 4H 50M 0S Something #1 At home       11