【问题标题】:Select specific rows based on previous row value (in the same column)根据前一行值选择特定行(在同一列中)
【发布时间】:2016-08-19 00:52:00
【问题描述】:

我一直在想办法通过 R 编写脚本,但就是不明白。我有一个这样的数据集:

Trial  Type Correct Latency     
1       55  0       0
3       30  1       766
4       10  1       344
6       40  1       716
7       10  1       326
9       30  1       550
10      10  1       350
11      64  0       0
13      30  1       683
14      10  1       270
16      30  1       666
17      10  1       297
19      40  1       616
20      10  1       315
21      64  0       0
23      40  1       850
24      10  1       322
26      30  1       566
27      20  0       766
28      40  1       500
29      20  1       230

持续时间更长(大约 1000 行)。

从这个数据集中,我想创建 4 个单独的 data.frames/tables 我可以导出表格以及进行我自己的计算

我想要一个 data.frame(总共 4 个),每个要点一个:

  • 输入 10 行,前面是 30 行
  • 输入 10 行,前面是 40 行
  • 类型 20 行,前面是类型 30 行
  • 类型 20 行前面是类型 40 行

我希望将相关行中的所有列放入这些新表中,但仅包括行类型 10 或 20 的列信息。

例如,根据示例数据,第一个表(类型 10 前面是类型 30)会这样:

Trial  Type Correct Latency     
  4       10     1       344
  10      10     1       350
  14      10     1       270
  17      10     1       297

第二张表(类型 10 前面是类型 40):

Trial    Type  Correct  Latency     
  7       10     1       326
  20      10     1       315
  24      10     1       322

第三张表(类型 20 前面是类型 30):

Trial    Type  Correct  Latency     
  27      20     0       766

第四张表(表 20 前面有类型 40):

Trial    Type  Correct   Latency        
 29      20      1        230

我可以很好地获取一个仅类型为 10 行的表和另一个类型为 20 行的表,但我无法弄清楚如何根据先前的类型值为类型 10 和 20 行创建不同的表。此外,一个问题是“试验”不按顺序排列(跳过数字)。

任何帮助将不胜感激。谢谢你。

另外,有没有办法也包含上一行,所以第四个表的输出看起来像这样:

第四张表(表 20 前面有类型 40):

Trial    Type  Correct   Latency        
 28      40      1        500
 29      20      1        230

【问题讨论】:

    标签: r loops dataframe row


    【解决方案1】:

    对于第四个示例,您可以将 which() 与来自 dplyrlag() 结合使用,以获得符合您条件的索引。然后你可以使用这些来子集data.frame

    # Get indices of rows that meet condition
    ind2 <- which(df$Type==20 & dplyr::lag(df$Type)==40)
    # Get indices of rows before the ones that meet condition
    ind1 <- which(df$Type==20 & dplyr::lag(df$Type)==40)-1
    
    # Subset data
    > df[c(ind1,ind2)]
       Trial Type Correct Latency
    1:    28   40       1     500
    2:    29   20       1     230
    

    【讨论】:

    • 您好 mtoto,我已经尝试了代码,但我只是得到一个空表(只有列标题)。关于我可能做错的任何建议?谢谢。
    • 我发现了问题,我们需要使用来自dplyrlag(),而不是来自stats 包。答案已更新。
    • 你也可以在dplyr使用filter(df,Type==10 &amp; lag(Type)==30)完全做到这一点
    • lag 函数执行类似d$Type == 10 &amp; c(NA,d$Type[-length(d$Type)]) == 30 的操作。它将向量向后移动一个位置,并删除最后一个值。
    • 谢谢,所有这些建议都奏效了!有没有办法在新表中也包含上一行?因此,例如,“Type 10 before Type 20”表的每一行不仅有 Type 10 行,而且每个 type 20 行?我在正文中进行了编辑。
    【解决方案2】:

    如果您总是想删除数据的第一次试验,这里有一个示例代码。

    var1 <- c(1,2,1,2,1,2,1,2,1,2)
    var2 <- c(1,1,1,2,2,2,2,3,3,3)
    
    dat <- data.frame(var1, var2)
    
    var1 var2
    1     1    1
    2     2    1
    3     1    1
    4     2    2
    5     1    2
    6     2    2
    7     1    2
    8     2    3
    9     1    3
    10    2    3
    
    #delete only this line directly
    filter(dat,lag(var2)==var2)
    
    var1 var2
    1     1    1
    2     2    1
    3     1    1
    6     2    2
    7     1    2
    10    2    3
    
    #delete the first 2 trials
    #make a list of all rows where var2[n-1]!=var2[n] --> using lag from dplyr
    drops <- c(1,2,which(lag(dat$var2)!=dat$var2), which(lag(dat$var2)!=dat$var2)+1)
    if (!identical(drops,numeric(0))) { dat <- dat[-drops,] }
    
    var1 var2
    3     1    1
    6     2    2
    7     1    2
    10    2    3
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-17
      • 2016-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多