获取特定列的最大值

时间:2014-12-14 14:44:30

标签: r

我有一个类似的文件:

"run","tick"
"1","0"
"1","1"
  ...
"1","289"       <-------------
"2","0"
  ...
"2","337"       <-------------
  ...

我想获得某个“运行”的最大“勾号”,如下所示:

for(run==1) 
   max = max(tick) 

我怎样才能在R中这样做?

1 个答案:

答案 0 :(得分:2)

你可以尝试

aggregate(tick~run, df, max)
#   run tick
#1   1  289
#2   2  337

或者

library(data.table)
setDT(df)[, list(Max=max(tick)), by=run]
#   run Max
#1:   1 289
#2:   2 337

或者

 library(dplyr)
 df %>%
    group_by(run)
    summarise(Max=max(tick))

或@Joshua Ulrich评论

 with(df, ave(tick, run, FUN=max))
 #[1] 289 289 289 337 337

或者来自@David Arenburg的评论

 with(df, tapply(tick, run, max))

数据

df <- structure(list(run = c(1L, 1L, 1L, 2L, 2L), tick = c(0, 1, 289, 
 0, 337)), .Names = c("run", "tick"), class = "data.frame", row.names = c(NA, 
-5L))