使用ivot_wider后删除小标题中的NA

时间:2019-10-11 19:10:19

标签: r tidyr

我正在尝试从一个数据集中创建一个表,该表从一个变量中获取两个因素,将它们进行更广泛的旋转,然后将它们排成一行。不幸的是,我要么继续制作两个单独的列表,要么得到:

dput(head(test1, 5))

# Edited section:
test1 <- df %>%   # Code used to create the table below
  select(`Incident ID`,`Device Time`, Description, `Elapsed Time`) %>% 
  filter(Description == "CPR Stopped" | Description == "CPR Started") %>% 
  mutate(Index = c(1:16)) %>% 
  pivot_wider(names_from = Description,
              values_from = c(`Elapsed Time`, `Device Time`)) %>% 
  filter(!is.na(test1))

dput(head(test1, 5))

  `Incident ID` Index `Elapsed Time_CPR Started` `Elapsed Time_CPR Stopped` `Device Time_CPR Started` `Device Time_CPR Stopped`
  <chr>         <int> <time>                     <time>                     <time>                    <time>                   
1 F190158585        1 01'03"                        NA                      18'37"                       NA                    
2 F190158585        2    NA                      01'08"                        NA                     18'42"                   
3 F190158585        3 01'34"                        NA                      19'08"                       NA                    
4 F190158585        4    NA                      03'47"                        NA                     21'22"                   
5 F190158585        5 04'00"                        NA                      21'35"                       NA  

我正在尝试获取一个看起来像这样的表:

df <- data.frame(Index = c(1:3),
                 CPR_Started = c("00:01:00", "00:02:03", "00:05:46"),
                 CPR_Stopped = c("00:01:53", "00:04:30", "00:08:00"))
print(df)

  Index CPR_Started CPR_Stopped
1     1    00:01:00    00:01:53
2     2    00:02:03    00:04:30
3     3    00:05:46    00:08:00

0 个答案:

没有答案