Subsetting a dataframe based on daily maxima

后端 未结 4 418
遥遥无期
遥遥无期 2021-01-27 05:15

I have an excel csv with a date/time column and a value associated with that date/time. I\'m trying to write a script that will go through this format (see below), and find 1) t

4条回答
  •  天命终不由人
    2021-01-27 05:49

    A solution using the plyr package, which I find very elegant for problems like this.

    dat.str <- '         V1    V2 V3
    1  5/1/2012  3:00  1
    2  5/1/2012  6:00  2
    3  5/1/2012  9:00  5
    4  5/1/2012 12:00  3
    5  5/1/2012 15:00  6
    6  5/1/2012 18:00  2
    7  5/1/2012 21:00  1
    8  5/2/2012  0:00  2
    9  5/2/2012  3:00  3
    10 5/2/2012  6:00  6
    11 5/2/2012  9:00  4
    12 5/2/2012 12:00  6
    13 5/2/2012 15:00  7
    14 5/2/2012 18:00  9
    15 5/2/2012 21:00  1'
    
    dat <- read.table(textConnection(dat.str), row.names=1, header=TRUE)
    
    library(plyr)
    ddply(dat, .(V1), function(x){
       x[which.max(x$V3), ]
    })
    

提交回复
热议问题