【问题标题】:R For loop fails applying max functionR For循环无法应用最大功能
【发布时间】:2016-09-19 18:16:38
【问题描述】:

我假设我是 R 新手,实际上我正在尝试了解基础知识。 目前我正在处理一个大型数据框(称为“ppl”),我必须对其进行编辑才能过滤一些行。每一行都包含在一个组中,并由一个强度(进入)值和一个样本值来表征。

       mz  rt      into   sample  tracker     sn   grp
 100.0153 126  2.762664      3    11908 7.522655   0
 100.0171 127  2.972048      2    5308  7.718521   0
 100.0788 272 30.217969      2    5309 19.024807   1
 100.0796 272 17.277916      3   11910  7.297716   1
 101.0042 128 37.557324      3   11916 27.991320   2
 101.0043 128 39.676014      2    5316 28.234918   2

嗯,第一个问题是:“如何从每组中选择强度最高的样本?” 我尝试了一个 for 循环:

for (i in ppl$grp) {
temp<-ppl[ppl$grp == i,]
sel<-rbind(sel,temp[max(temp$into),])
}

事实上它适用于 ppl$grp == 0,但接下来的循环返回 NAs 行。 然后过滤的数据框(称为“sel”)也应该存储删除行的样本值。应该是这样的:

      mz  rt      into   sample  tracker     sn   grp
100.0171 127  2.972048   c(2,3)    5308  7.718521   0
100.0788 272 30.217969   c(2,3)    5309 19.024807   1
101.0043 128 39.676014   c(2,3)    5316 28.234918   2

为了得到这个,我会使用这种方法:

lev<-factor(ppl$grp)
samp<-ppl$sample
samp2<-split(samp,lev)
sel$sample<-samp2

有什么提示吗?因为我还没有解决上一个问题,所以我无法测试它。

非常感谢。

【问题讨论】:

    标签: r for-loop split max


    【解决方案1】:

    不确定我是否遵循您的问题。但也许这会让你开始。

    library(dplyr)
    ppl %>% group_by(grp) %>% filter(into == max(into)) 
    

    【讨论】:

      【解决方案2】:

      使用avebase R 选项是

      ppl[with(ppl, ave(into, grp, FUN = max)==into),]
      

      如果预期输出中的'sample'列在每个'grp'中都有unique元素,则在按'grp'分组后,将'sample'更新为pasted unique'的元素sample',然后arrange 'into' 降序和slice 第一行。

      library(dplyr)
      ppl %>%
          group_by(grp) %>% 
          mutate(sample = toString(sort(unique(sample)))) %>% 
          arrange(desc(into)) %>%
          slice(1L)
      #       mz    rt      into sample tracker        sn   grp
      #     <dbl> <int>     <dbl>  <chr>   <int>     <dbl> <int>
      #1 100.0171   127  2.972048   2, 3    5308  7.718521     0
      #2 100.0788   272 30.217969   2, 3    5309 19.024807     1
      #3 101.0043   128 39.676014   2, 3    5316 28.234918     2
      

      【讨论】:

        【解决方案3】:

        data.table 替代方案:

        library(data.table)
        setkey(setDT(ppl),grp)
        ppl <- ppl[ppl[,into==max(into),by=grp]$V1,]
        ##         mz  rt      into sample tracker        sn grp
        ##1: 100.0171 127  2.972048      2    5308  7.718521   0
        ##2: 100.0788 272 30.217969      2    5309 19.024807   1
        ##3: 101.0043 128 39.676014      2    5316 28.234918   2
        

        【讨论】:

          【解决方案4】:

          我不知道为什么这段代码会起作用

          for (i in ppl$grp) {
            temp<-ppl[ppl$grp == i,]
            sel<-rbind(sel,temp[max(temp$into),])
          }
          

          max(temp$into) 应该返回最大值——在大多数情况下它似乎不是整数。

          此外,在每个 for 循环实例中使用 rbind 构建一个 data.frame 不是好的做法(在任何语言中)。它需要放弃一些类型检查和数组增长,这可能会变得非常昂贵。

          此外,当该组有任何 NA 时,max 将返回 NA。

          还有一个问题是关于你想对领带做什么?您只想要一个结果还是全部? Akrun 提供的代码将为您提供所有这些。

          此代码将编写一个具有组 max 的新列

           ppl$grpmax <- ave(ppl$into, ppl$grp, FUN=function(x) { max(x, na.rm=TRUE ) } )
          

          然后您可以选择组中等于最大值的所有值

          pplmax <- subset(ppl, into == grpmax)
          

          如果您只希望每组一个,则可以删除重复项

          pplmax[!duplicated(pplmax$grp),]
          

          【讨论】:

            猜你喜欢
            • 2011-10-01
            • 2020-10-06
            • 1970-01-01
            • 2015-08-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-17
            • 1970-01-01
            相关资源
            最近更新 更多