如何在新列中将类别分配给 R 数据框中的特定行?

How to assign category to specific rows in R data frame, in a new column?

在时间序列数据集中,什么是对行集进行分组以使其在新列中具有唯一标识符的好方法?例如(以一种非常简洁的方式),取这个:

library(tidyverse)

data <- read_csv("snippet.csv")

print(data,n=29)

# A tibble: 29 x 5
   Port        Timestamp             MultiPort dev_value dev_unit
   <chr>       <chr>                 <chr>         <dbl> <chr>   
 1 PortConRef1 2/26/2020 12:39:40 PM n            -38.1  ‰       
 2 PortConRef1 2/26/2020 12:39:41 PM n            -38.0  ‰       
 3 PortConRef1 2/26/2020 12:39:42 PM n            -38.2  ‰       
 4 PortConRef1 2/26/2020 12:39:43 PM n            -38.1  ‰       
 5 PortConRef1 2/26/2020 12:39:44 PM n            -38.3  ‰       
 6 PortConRef1 2/26/2020 12:39:45 PM n            -37.9  ‰       
 7 PortConRef1 2/26/2020 12:39:46 PM n            -38.3  ‰       
 8 PortRef1    2/26/2020 12:40:48 PM n             -9.82 ‰       
 9 PortRef1    2/26/2020 12:40:49 PM n            -10.2  ‰       
10 PortRef1    2/26/2020 12:40:50 PM n             -9.75 ‰       
11 PortRef1    2/26/2020 12:40:51 PM n             -9.89 ‰       
12 PortRef1    2/26/2020 12:40:52 PM n            -10.1  ‰       
13 PortRef1    2/26/2020 12:40:53 PM n            -10.1  ‰       
14 PortRef1    2/26/2020 12:40:54 PM n            -10.3  ‰       
15 PortSampleB 2/26/2020 12:51:14 PM n             -5.13 ‰       
16 PortSampleB 2/26/2020 12:51:15 PM n             -4.70 ‰       
17 PortSampleB 2/26/2020 12:51:16 PM n             -4.90 ‰       
18 PortSampleB 2/26/2020 12:51:17 PM n             -5.03 ‰       
19 PortSampleB 2/26/2020 12:51:18 PM n             -4.76 ‰       
20 PortSampleB 2/26/2020 12:52:50 PM y             -5.15 ‰       
21 PortSampleB 2/26/2020 12:52:51 PM y             -4.97 ‰       
22 PortSampleB 2/26/2020 12:52:52 PM y             -5.11 ‰       
23 PortSampleB 2/26/2020 12:52:53 PM y             -4.71 ‰       
24 PortSampleB 2/26/2020 12:58:49 PM y             -5.19 ‰       
25 PortSampleB 2/26/2020 1:00:21 PM  n             -4.75 ‰       
26 PortSampleB 2/26/2020 1:00:22 PM  n             -5.20 ‰       
27 PortSampleB 2/26/2020 1:00:23 PM  n             -4.95 ‰       
28 PortSampleB 2/26/2020 1:00:24 PM  n             -5.06 ‰       
29 PortSampleB 2/26/2020 1:00:25 PM  n             -4.81 ‰ 

# Remove reference gas rows
data2 <- data %>% 
  filter(`Port`=="PortSampleB")

# Convert timestamp column to useable time
library(lubridate)
data2 <- data2 %>% 
  mutate(
    time=mdy_hms(`Timestamp`))

> print(data2,n=15)
# A tibble: 15 x 6
   Port        Timestamp             MultiPort dev_value dev_unit time               
   <chr>       <chr>                 <chr>         <dbl> <chr>    <dttm>             
 1 PortSampleB 2/26/2020 12:51:14 PM n             -5.13 ‰        2020-02-26 12:51:14
 2 PortSampleB 2/26/2020 12:51:15 PM n             -4.70 ‰        2020-02-26 12:51:15
 3 PortSampleB 2/26/2020 12:51:16 PM n             -4.90 ‰        2020-02-26 12:51:16
 4 PortSampleB 2/26/2020 12:51:17 PM n             -5.03 ‰        2020-02-26 12:51:17
 5 PortSampleB 2/26/2020 12:51:18 PM n             -4.76 ‰        2020-02-26 12:51:18
 6 PortSampleB 2/26/2020 12:52:50 PM y             -5.15 ‰        2020-02-26 12:52:50
 7 PortSampleB 2/26/2020 12:52:51 PM y             -4.97 ‰        2020-02-26 12:52:51
 8 PortSampleB 2/26/2020 12:52:52 PM y             -5.11 ‰        2020-02-26 12:52:52
 9 PortSampleB 2/26/2020 12:52:53 PM y             -4.71 ‰        2020-02-26 12:52:53
#...
10 PortSampleB 2/26/2020 12:58:49 PM y             -5.19 ‰        2020-02-26 12:58:49
11 PortSampleB 2/26/2020 1:00:21 PM  n             -4.75 ‰        2020-02-26 13:00:21
12 PortSampleB 2/26/2020 1:00:22 PM  n             -5.20 ‰        2020-02-26 13:00:22
13 PortSampleB 2/26/2020 1:00:23 PM  n             -4.95 ‰        2020-02-26 13:00:23
14 PortSampleB 2/26/2020 1:00:24 PM  n             -5.06 ‰        2020-02-26 13:00:24
15 PortSampleB 2/26/2020 1:00:25 PM  n             -4.81 ‰        2020-02-26 13:00:25    

#note that data from the original dataset has been removed between rows 9 and 10 to ease reproducibility

并给每个部分(由行号或 'time' 定义)一个唯一的字母类别。在上面的简化示例中,第 5 行和第 6 行之间的 1 Hz 数据收集存在间隙,对应于从 "n" 到 "y" 的多端口切换。这种模式每六分钟重复一次,因此在完整的数据集中有八个交替的 6 分钟组 "n" 和 "y" 之间有 90 秒。由于是1hz的数据,每6分钟一组有360行。

我希望每六分钟的 y 和 n 时间段有一个不同的字母类别,例如 "a" 到 "h"。

目标是为每个时间段的数据绘制一个单独的箱线图,以绘制在原始数据之上,它看起来像这样:

我们可以使用 data.table 中的 rleid 来在每次 MultiPort 变化时得到一个唯一的数字,并用它来索引预定义的 letters 向量。

library(dplyr)
df %>% mutate(cat = letters[data.table::rleid(MultiPort)])

我们可以使用 rle 来自 base R

df$cat <- letters[with(rle(df$MultiPort), rep(seq_along(values), lengths))]

作为更新,这是我用来将箱形图绘制到时间序列上的内容。


#from Ronak's answer
df1 <- df %>% mutate(cat=letters[data.table::rleid(MultiPort)])


df1 %>% 
  ggplot(mapping=aes(x=`time`,y=`dev_value`))+
  geom_point()+
  geom_boxplot(aes(data=cat,fill=MultiPort))+
  ylab("13C vs Intl. Std. (‰)")+
  xlab("Time")+
  theme(legend.text = element_text(size=12),
        legend.title = element_text(size = 14),
        axis.title.x = element_text(size=20),
        axis.title.y = element_text(size=20),
        axis.text.x = element_text(size=14,colour="black"),
        axis.text.y = element_text(size=18,colour="black"),
        legend.box.background = element_rect(color="black", size=2))+
  scale_fill_manual(values=c("dodgerblue1","red"),labels=c("Reference","Sample"))+
  theme(plot.tag.position = c(0.8, 0.02))