【问题标题】:How to list min and max values for multiple values of a given name in a data frame in R?如何在R中的数据框中列出给定名称的多个值的最小值和最大值?
【发布时间】:2021-11-14 14:55:09
【问题描述】:

我有一个数据框,其中包含一个“名称”列,其中包含多个给定名称的实例,另一列“时间”有不同的时间。我正在尝试将此原始数据转换为另一个数据框,该数据框仅列出名称的唯一值,并在单独的列中给出时间的最小值和最大值,以及原始数据框中每个名称的实例计数。 我希望最终的数据帧按 min.time 排序,从小到大。

例如:

到目前为止,我已经获得了唯一的名称列表和每个名称的计数,但我不知道如何找到每个唯一名称的最小和最大时间值。任何建议将不胜感激。

【问题讨论】:

  • 如果您创建一个可重现的示例以及预期的输出,这将更容易提供帮助。了解如何给出一个可重现的例子stackoverflow.com/questions/5963269/…
  • aggregate(time~name, df, \(x)c(length(x), range(x)))
  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: r dataframe max min


【解决方案1】:

使用 tidyverse:

library(tidyverse)

df <- 
tribble(
  ~name, ~time,
  'a', 19.642,
  'a', 19.644,
  'b', 20.178,
  'c', 22.345,
  'b', 20.183,
  'a', 19.646,
  'b', 20.190,
  'c', 22.332)

df %>% 
  group_by(name) %>% 
  summarise(count = n(), min.time = min(time), max.time = max(time)) %>% 
  arrange(min.time)
#> # A tibble: 3 × 4
#>   name  count min.time max.time
#>   <chr> <int>    <dbl>    <dbl>
#> 1 a         3     19.6     19.6
#> 2 b         3     20.2     20.2
#> 3 c         2     22.3     22.3

reprex package (v2.0.0) 于 2021-09-20 创建

【讨论】:

  • 谢谢,我使用您的解决方案和 jared_mamrot 的回答来指导我和我自己的一些更改以产生我所追求的最终结果。见下文,感谢您的帮助。
【解决方案2】:

使用dplyrdataset %&gt;% group_by(name) %&gt;% summarize(n= n(), min = min(time), max = max(time))

【讨论】:

    【解决方案3】:

    编辑

    根据您在下面的评论,这是一个潜在的解决方案:

    library(tidyverse)
    df <- data.frame(name = c("a", "a", "b", "c", "b", "a", "b", "c"),
                     time = c(19.642, 19.644, 20.178, 22.345,
                              20.183, 19.646, 20.190, 22.332))
    df2 <- df %>%
      group_by(name) %>%
      summarise(n = n(),
                min = min(time),
                max = max(time))
    print.data.frame(df2)
    #>   name n    min    max
    #> 1    a 3 19.642 19.646
    #> 2    b 3 20.178 20.190
    #> 3    c 2 22.332 22.345
    
    df3 <- df2 %>%
      arrange(desc(min)) %>%
      mutate(`max - min` = max - min)
    print.data.frame(df3)
    #>   name n    min    max max - min
    #> 1    c 2 22.332 22.345     0.013
    #> 2    b 3 20.178 20.190     0.012
    #> 3    a 3 19.642 19.646     0.004
    

    reprex package (v2.0.1) 于 2021 年 9 月 21 日创建

    【讨论】:

    • 谢谢,这很好,只是它按名称排序输出,而不是按最短时间排序。另外,如果我想向 df2 添加第四列,即 max-min,我该怎么做?
    • 编辑了我的答案以提供潜在的解决方案。
    • 谢谢,我最终想出了自己的解决方案来添加额外的列并按分钟排序。见下文。感谢您的帮助。
    【解决方案4】:

    这是我最终使用的解决方案,它借鉴了上面 jpdugo17 和 jared_mamrot 的答案。

    library(readr)
    library(tidyverse)
    
    #Objective is to read raw data in format "compound, elution time, sample,
    #datafile, ARL number, notes" and output a new data file in format "unique
    #compound list, count, min elution time, max elution time, elution span", sorted 
    #from lowest to highest min elution time, and excluding non-blank 'notes' fields.
    
    raw_data<-read_csv("GC3_raw_data.csv") #reads raw data in from .csv file
    df_in<-data.frame(raw_data)  #turns raw data into a data.frame
    df_in<-df_in[is.na(df_in$Notes),] #removes entries that have notes
    df_out <- df_in %>%
      group_by(Compound)%>%
      summarise(n=n(),
                min.elution.time = min(Elution.time),
                max.elution.time = max(Elution.time)) #creates a new dataframe with unique compound list and min/max elution times
    
    df_out$elution.span <- df_out$max.elution.time - df_out$min.elution.time  #adds the elution span column to the new dataframe
    
    df_out <- df_out[order(df_out$min.elution.time),] #orders the new dataframe by min.elution.time, from smallest to largest
    
    print.data.frame(df_out)
    
    write.csv(df_out,"GC3_clean_data.csv")
    
    

    reprex package (v2.0.1) 于 2021 年 9 月 22 日创建

    【讨论】:

      猜你喜欢
      • 2015-07-29
      • 2021-08-02
      • 1970-01-01
      • 2020-01-10
      • 2013-06-05
      • 1970-01-01
      • 2017-12-29
      • 2021-02-08
      • 1970-01-01
      相关资源
      最近更新 更多