【问题标题】:Count number number of rows until value is reached and do this within specific ranged within a dataset计算行数直到达到值并在数据集中的特定范围内执行此操作
【发布时间】:2015-03-29 04:05:49
【问题描述】:

我是初学者 R 用户(不完全确定它是否是最适合这种用途的语言)并且很难想出一个我认为可能相对容易的解决方案。

编辑 - 我的数据的前 60 帧如下所示:

structure(list(Filename = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L), .Label = c("BJB1NRL.mat", "DJG1NRL.mat", 
"DMA1NRL.mat", "DWS1NRL.mat", "EJC1NRL.mat", "FMB1NRL.mat", "GPC1NRL.mat", 
"GZZ1NRL.mat", "JMC1NRL.mat", "JTH1NRL.mat", "KAN1NRL.mat", "MAS1NRL.mat", 
"MFM1NRL.mat", "MJU1NRL.mat", "OVK1NRL.mat", "PCA1NRL.mat", "RHG1NRL.mat", 
"RHM1NRL.mat", "RJS1NRL.mat", "SIS1NRL.mat", "SXV1NRL.mat", "TXN1NRL.mat"
), class = "factor"), Label = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "V", class = "factor"), 
    seg_Start = c(231.537, 231.537, 231.537, 231.537, 231.537, 
    231.537, 231.537, 231.537, 231.537, 231.537, 231.537, 231.537, 
    231.537, 231.537, 231.537, 231.537, 231.537, 231.537, 231.537, 
    231.537, 231.537, 231.537, 554.141, 554.141, 554.141, 554.141, 
    554.141, 554.141, 554.141, 554.141, 554.141, 554.141, 554.141, 
    554.141, 554.141, 554.141, 554.141, 554.141, 554.141, 554.141, 
    554.141, 554.141, 554.141, 554.141, 554.141, 554.141, 554.141, 
    554.141, 554.141, 554.141, 554.141, 554.141, 554.141, 554.141, 
    554.141, 554.141, 554.141, 1081.457, 1081.457, 1081.457), 
    seg_End = c(444.039, 444.039, 444.039, 444.039, 444.039, 
    444.039, 444.039, 444.039, 444.039, 444.039, 444.039, 444.039, 
    444.039, 444.039, 444.039, 444.039, 444.039, 444.039, 444.039, 
    444.039, 444.039, 444.039, 887.246, 887.246, 887.246, 887.246, 
    887.246, 887.246, 887.246, 887.246, 887.246, 887.246, 887.246, 
    887.246, 887.246, 887.246, 887.246, 887.246, 887.246, 887.246, 
    887.246, 887.246, 887.246, 887.246, 887.246, 887.246, 887.246, 
    887.246, 887.246, 887.246, 887.246, 887.246, 887.246, 887.246, 
    887.246, 887.246, 887.246, 1269.05, 1269.05, 1269.05), t_ms = c(230L, 
    240L, 250L, 260L, 270L, 280L, 290L, 300L, 310L, 320L, 330L, 
    340L, 350L, 360L, 370L, 380L, 390L, 400L, 410L, 420L, 430L, 
    440L, 550L, 560L, 570L, 580L, 590L, 600L, 610L, 620L, 630L, 
    640L, 650L, 660L, 670L, 680L, 690L, 700L, 710L, 720L, 730L, 
    740L, 750L, 760L, 770L, 780L, 790L, 800L, 810L, 820L, 830L, 
    840L, 850L, 860L, 870L, 880L, 890L, 1080L, 1090L, 1100L), 
    CPP = c(0, 0, 19.553, 16.063, 18.09, 21.547, 21.37, 21.616, 
    18.125, 20.936, 20.134, 23.642, 23.319, 24.674, 22.299, 22.587, 
    27.477, 25.924, 20.166, 20.195, 16.807, 18.189, 16.843, 16.07, 
    20.384, 26.11, 26.536, 28.891, 26.769, 27.622, 28.303, 24.373, 
    22.043, 23.579, 26.797, 26.384, 29.34, 31.437, 31.531, 24.404, 
    22.396, 26.416, 28.531, 26.735, 28.796, 28.494, 27.177, 27.325, 
    26.273, 29.066, 24.344, 21.124, 28.363, 24.193, 19.815, 11.897, 
    14.125, 0, 0, 22.398)), .Names = c("Filename", "Label", "seg_Start", 
"seg_End", "t_ms", "CPP"), row.names = c(NA, 60L), class = "data.frame")

我要做的就是直接手动执行,但我正在努力创建一个脚本来为我执行此操作。 对于每个时间段(即 231-444,然后是 1081-1269 等),我需要查看最后一列(CPP)中的值达到阈值(例如 28)需要多少毫秒。 也就是说,我需要计算从段开始(t_ms = seg_Start)直到最后一行中的值达到这个阈值的行数,因为每一行都是10ms。 如果该值从未达到阈值,则应给出 0 值。

到目前为止,我的代码主要是我尝试将数据集相应地拆分以进行分析。它可能效率低下,更令人担忧的是,不正确。

编辑-为了澄清-我需要分别分析每个“段”的“CPP”值,每个不同的“文件名”。一个段是 't_ms' 中 'seg_Start' 和 'seg_End' 之间的值。

例如,对于第一个文件名的第一段,我的答案是“280 毫秒”——每行是 10 毫秒。在第一个 seg_start - seg_End 之间的时间段中,CPP 第一次达到 28 位于第 28 行(值 = 28.891)。 10 x 28 = 280 毫秒。

最终编辑删除了无关的数据集。左侧部分仅与问题相关。

【问题讨论】:

    标签: r excel matlab


    【解决方案1】:

    编辑:

    我编辑了您的数据以包含两个不同的文件名,如下所示。

    df$Filename[15:17] <- "DMA1NRL.mat"
    

    time.2.threshold 是一个接受三个参数的函数:df - 数据,threshold - CPP 阈值,time_factor - 毫秒数。

    在函数内部,有三个循环。第一个循环处理文件名的数据。第二个循环处理 seg_Start 中的值,第三个最内层循环处理通过第二个循环获得的数据帧,并对 CPP 列的数据帧进行排序,并找到达到 CPP 阈值及以上所需的最小行值(行索引) .

    time_2_threshold 是 time_factor * rows_CPP_to_threshold

    最后,time.2.threshold 函数返回一个数据帧,其中包含整个数据帧的文件名、seg_Start、seg_End、rows_CPP_to_threshold、total_rows 和 time_2_threshold 值。

    我仍在尝试了解您的期望,因为您的问题尚不明显,但步骤已正确编码。人们可能会建议一些可以提高性能的代码的惯用 R 版本,但我不在这里这样做,因为目前还不清楚您的期望是什么。希望这会有所帮助!

    time.2.threshold <- function(df = df, threshold = 25, time_factor = 10){
      
      df_threshold <- data.frame(filename = character(),
                                 seg_Start = numeric(),
                                 seg_End = numeric(),
                                 rows_CPP_to_threshold = numeric(),
                                 total_rows = numeric(),
                                 time_2_threshold = numeric(),
                                 stringsAsFactors = FALSE)
      
      for(df_file in unique(df$Filename)){
        
        df_filename <- df[which(df$Filename == df_file), ]
        
        for (i in unique(df$seg_Start)){
          
          start_df <- df_filename[which(df_filename$seg_Start == i), ]
          
          seg_End_unique <- unique(start_df$seg_End)
          
          if(length(seg_End_unique) > 0 ){
            
            for(j in seg_End_unique){
              
              df1 <- start_df[which(start_df$seg_End == j), ]
              
              #df1 <- df1[with(df1, order(CPP)),]
              
              df1_CPP_index = which(df1$CPP >= threshold)
              
              if( length(df1_CPP_index) > 0 && !is.na(df1_CPP_index) && !is.nan(df1_CPP_index) ){
                rows_CPP_to_threshold <- min(df1_CPP_index)
              }else{
                rows_CPP_to_threshold <- NA
              }
              
              total_rows = nrow(df1)
              
              df2 <- data.frame(filename = df_file,
                                seg_Start = i,
                                seg_End = j,
                                rows_CPP_to_threshold = rows_CPP_to_threshold,
                                total_rows = total_rows,
                                time_2_threshold = time_factor*rows_CPP_to_threshold,
                                stringsAsFactors = FALSE)
              
              df_threshold <- rbind(df_threshold, df2 )
            }    
          }else
            next
        }
      }
      return(df_threshold)
    }
    
    df_threshold <- time.2.threshold(df = df, threshold = 15, time_factor = 10)
    

    输出

    df_threshold <- time.2.threshold(df = df, threshold = 15, time_factor = 10)
    
        print(df_threshold)
         filename seg_Start  seg_End rows_CPP_to_threshold total_rows time_2_threshold
    1 BJB1NRL.mat   231.537  444.039                     3         19               30
    2 BJB1NRL.mat   554.141  887.246                     1         35               10
    3 BJB1NRL.mat  1081.457 1269.050                     3          3               30
    4 DMA1NRL.mat   231.537  444.039                     1          3               10
    

    注释掉CPP列排序后:

    df_threshold <- time.2.threshold(df = df, threshold = 25, time_factor = 10)
    print(df_threshold)
         filename seg_Start  seg_End rows_CPP_to_threshold total_rows time_2_threshold
    1 BJB1NRL.mat   231.537  444.039                    15         19              150
    2 BJB1NRL.mat   554.141  887.246                     4         35               40
    3 BJB1NRL.mat  1081.457 1269.050                    NA          3               NA
    4 DMA1NRL.mat   231.537  444.039                     3          3               30
    

    【讨论】:

    • 感谢您的回复,我可以看到您做了什么,最终效率如何。但是,我想我不清楚我需要衡量什么,抱歉。我的数据集大约有 18,000 行,分成 22 个不同的“文件名”(这些文件名是不同的人,所以需要单独分析)。对于 seg_Start 和 seg_End 之间的每次时间(即在第一个示例中,从 230ms
    • 使用此命令提供一些数据,这样使用起来会更容易。dput(head(df, 50)) 将输出数据的前 50 行。在上面的问题中复制并粘贴输出。
    • 如果我理解正确,您首先将 .csv 文件中的数据读取到名称为“Result”的数据框中。如果是这样,您需要使用 dput(head(Result, 50)) 来提供数据的前 50 行。
    • @Linguistable 请参阅上面的答案。该代码假定您在数据集中有一个文件名。如果您有多个文件名,也可以轻松调整此代码以适应它们。
    • 谢谢萨西什。您上面的代码让我非常沮丧地接近我正在寻找的东西。我可以看到 time_2_threshold 中的输出值是具有 CPP 达到阈值时的第一个实例的行。我认为这包含我需要的信息,但并不完全存在!当我在 RStudio 中查看()时,我可以看到 time_2_threshold 值具有 df 中原始行的 row.name(例如,第一个 time_2_threshold 值为 21 的 row.name 为 18)。我需要的是这个原始的 row.name,因为它告诉我花了多长时间(以毫秒为单位)。在此期间我会继续尝试调整...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 2019-01-27
    • 2013-05-29
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    相关资源
    最近更新 更多