如何在单独的列中基于起止信息创建值

时间:2019-04-22 19:53:10

标签: r dataframe dplyr tidyr

我有一个由研究设备创建的非常混乱的数据集。此数据显示每几毫秒(“时间”)的生理测量(“生理”)。输出列出了一些用户消息,例如试验何时开始(“ START_TRIAL n”),试验何时结束(“ STOP_TRIAL”)以及研究人员可能感兴趣的其他随机事件。有时,“ START_TRIAL n”消息会连续重复,有时没有消息时,在原本为空白单元格的地方会留下一个简单的“ 0”。

我希望创建一个新列,以表示当前案件属于哪个审判。 (请参见下面的示例数据)。

是否可以使用dplyr和mutate做到这一点?我想知道是否可能需要执行if-then语句来更改每种情况下新列的值,但是肯定有更优雅的解决方案吗? (预先感谢您帮助这个新手!)

Time    Physio  Cond
1   34  START_TRIAL 1
2   33  0
3   25  RANDOM_MSG
4   43  STOP_TRIAL
5   27  START_TRIAL 2
6   54  START_TRIAL 2
7   32  0
8   54  RANDOM_MSG
9   23  STOP_TRIAL

structure(list(Time = 1:9, Physio = c(34L, 33L, 25L, 43L, 27L, 
54L, 32L, 54L, 23L), Cond = structure(c(4L, 2L, 3L, 6L, 5L, 5L, 
2L, 3L, 6L), .Label = c("", "0", "RANDOM_MSG", "START_TRIAL 1", 
"START_TRIAL 2", "STOP_TRIAL"), class = "factor")), .Names = c("Time", 
"Physio", "Cond"), row.names = c(NA, 9L), class = "data.frame")

进入

Time    Physio  Trial   Cond
1   34  1   START_TRIAL 1
2   33  1   0
3   25  1   RANDOM_MSG
4   43  1   STOP_TRIAL
5   27  2   START_TRIAL 2
6   54  2   START_TRIAL 2
7   32  2   0
8   54  2   RANDOM_MSG
9   23  2   STOP_TRIAL

structure(list(Time = 1:9, Physio = c(34L, 33L, 25L, 43L, 27L, 
54L, 32L, 54L, 23L), Trial = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L), Cond = structure(c(4L, 2L, 3L, 6L, 5L, 5L, 2L, 3L, 6L), .Label = c("", 
"0", "RANDOM_MSG", "START_TRIAL 1", "START_TRIAL 2", "STOP_TRIAL"
), class = "factor")), .Names = c("Time", "Physio", "Trial", 
"Cond"), row.names = c(NA, 9L), class = "data.frame")

1 个答案:

答案 0 :(得分:3)

一种选择是用grep标识“ START_TRIAL”,执行match以获取索引,并fill将NA元素与先前的非NA相邻元素

library(dplyr)
library(tidyr)
df1 %>% 
   mutate(Trial = match(PhysioCond, unique(grep("START_TRIAL", 
             PhysioCond, value = TRUE)))) %>% 
   fill(Trial)
#    Time    PhysioCond Trial
#1   34 START_TRIAL 1     1
#2   33             0     1
#3   25    RANDOM_MSG     1
#4   43    STOP_TRIAL     1
#5   27 START_TRIAL 2     2
#6   54 START_TRIAL 2     2
#7   32             0     2
#8   54    RANDOM_MSG     2
#9   23    STOP_TRIAL     2

注意:不清楚列名,但是逻辑应该工作正常

数据

df1 <- structure(list(Time = c(34L, 33L, 25L, 43L, 27L, 54L, 32L, 54L, 
 23L), PhysioCond = c("START_TRIAL 1", "0", "RANDOM_MSG", "STOP_TRIAL", 
"START_TRIAL 2", "START_TRIAL 2", "0", "RANDOM_MSG", "STOP_TRIAL"
 )), class = "data.frame", row.names = c("1", "2", "3", "4", "5", 
  "6", "7", "8", "9"))