[R] Finding minimum of time subset
jim holtman
jholtman at gmail.com
Fri Aug 14 19:51:54 CEST 2009
sapply(mylist, '[', 1)
is equivalent to
sapply(mylist, function(x) x[1]) # select just the first element
"[" is an function that is called with a object and an index. Using
it the way I did in the email was a shorthand way of doing it. Here
is an example:
> x <- list(1,2,3)
> x[1]
[[1]]
[1] 1
> `[`(x, 1)
[[1]]
[1] 1
Notice the function call `[`(x,1). This is what is being done in the
sapply and passing the 1 as the second parameter.
On Fri, Aug 14, 2009 at 1:30 PM, Tim Clark<mudiver1200 at yahoo.com> wrote:
> Jim,
>
> That works great! However, would you please explain what the '[' and the 1 do in the sapply function? I understand that you are cutting x by quarter, then creating a list of x that is split based on those cuts. I just don't understand what "[" means in this contex, or what the number one at the end does.
>
> Thanks for you help,
>
> Tim
>
>
>
> Tim Clark
> Department of Zoology
> University of Hawaii
>
>
> --- On Fri, 8/14/09, jim holtman <jholtman at gmail.com> wrote:
>
>> From: jim holtman <jholtman at gmail.com>
>> Subject: Re: [R] Finding minimum of time subset
>> To: "Tim Clark" <mudiver1200 at yahoo.com>
>> Cc: r-help at r-project.org
>> Date: Friday, August 14, 2009, 6:18 AM
>> Here is one way to do it:
>>
>> >
>> mytime<-c("12:00:00","12:00:05","12:15:05","12:15:06","12:20:00","12:30:01","12:45:01","13:00:00","13:15:02")
>> > # you might want a date on your data
>> > x <- as.POSIXct(mytime, format="%H:%M:%S")
>> > # create quarter hour intervals for the data range
>> > quarter <- seq(trunc(min(x), 'days'), trunc(max(x)
>> + 86400, 'days'), by='15 min') # add 86400 to add a day for
>> truncation
>> > # cut the data by quarter hours and then take the
>> first value in each group
>> > x.s <- sapply(split(x, cut(x, breaks=quarter),
>> drop=TRUE), '[', 1)
>> > # lost the 'class' for some reason; put it back
>> > class(x.s) <- c("POSIXt", "POSIXct")
>> > # the answer
>> > x.s
>> 2009-08-14 12:00:00
>> 2009-08-14 12:15:00
>> 2009-08-14
>> 12:30:00 2009-08-14
>> 12:45:00 2009-08-14 13:00:00
>> "2009-08-14 12:00:00 EDT" "2009-08-14 12:15:05 EDT"
>> "2009-08-14
>> 12:30:01 EDT" "2009-08-14 12:45:01 EDT" "2009-08-14
>> 13:00:00 EDT"
>> 2009-08-14 13:15:00
>> "2009-08-14 13:15:02 EDT"
>> >
>>
>>
>> On Thu, Aug 13, 2009 at 4:10 PM, Tim Clark<mudiver1200 at yahoo.com>
>> wrote:
>> > Dear List,
>> >
>> > I have a data frame of data taken every few seconds.
>> I would like to subset the data to retain only the data
>> taken on the quarter hour, and as close to the quarter hour
>> as possible. So far I have figured out how to subset the
>> data to the quarter hour, but not how to keep only the
>> minimum time for each quarter hour.
>> >
>> > For example:
>> >
>> mytime<-c("12:00:00","12:00:05","12:15:05","12:15:06","12:20:00","12:30:01","12:45:01","13:00:00","13:15:02")
>> >
>> subtime<-grep(pattern="[[:digit:]]+[[:punct:]]00[[:punct:]][[:digit:]]+|[[:digit:]]+[[:punct:]]15[[:punct:]][[:digit:]]+|[[:digit:]]+[[:punct:]]30[[:punct:]][[:digit:]]+|[[:digit:]]+[[:punct:]]45[[:punct:]][[:digit:]]+",mytime)
>> > mytime[subtime]
>> >
>> > [1] "12:00:00" "12:00:05" "12:15:05" "12:15:06"
>> "12:30:01" "12:45:01" "13:00:00" "13:15:02"
>> >
>> > This gives me the data taken at quarter hour intervals
>> (removes 12:20:00) but I am still left with multiple values
>> at the quarter hours.
>> >
>> > I would like to obtain:
>> >
>> > "12:00:00" "12:15:05" "12:30:01" "12:45:01" "13:00:00"
>> "13:15:02"
>> >
>> > Thanks!
>> >
>> > Tim
>> >
>> >
>> >
>> >
>> > Tim Clark
>> > Department of Zoology
>> > University of Hawaii
>> >
>> > ______________________________________________
>> > R-help at r-project.org
>> mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> > and provide commented, minimal, self-contained,
>> reproducible code.
>> >
>>
>>
>>
>> --
>> Jim Holtman
>> Cincinnati, OH
>> +1 513 646 9390
>>
>> What is the problem that you are trying to solve?
>>
>
>
>
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list