Re: [R] handling the output of strsplit

From: Mark Difford <mark_difford_at_yahoo.co.uk>
Date: Sat, 21 Jun 2008 05:33:54 -0700 (PDT)

Hi Denis,

>> h = c("3h30", "6h30", "9h40", "11h25", "14h00", >> "15h55", "23h")

>> I could not figure out how to use chron to import this into times, so >> I tried to extract the hours and minutes on my own.

Look at ?strptime for this:
##
strptime("6h30", format="%Ih%M")
[1] "2008-06-21 06:30:00"

>> h2 = strsplit(h, "h")...
>> For this in particular, but lists in general, how can one extract the  
>> first item of each element in the list, then the second item of each  
>> element, etc.?

Look at ?"["

## Example: _note_ use of [[...]]
h2[[1]][1]
[1] "3"

h2[[1]][2]
[1] "30"

h2[[1]][1:2]
[1] "3" "30"

h2[[2]][1:2]
[1] "6" "30"

HTH, Mark.

Denis Chabot wrote:
>
> Hi,
>
> Simple question, but I did not figure out how to find the answer on my
> own (wrong choice of keywords on my part).
>
> I have a character variable for time of day that has entries looking
> like "6h30", "7h40", "12h25", "23h", etc. For the sake of this
> message, say
>
> h = c("3h30", "6h30", "9h40", "11h25", "14h00",
> "15h55", "23h")
>
> I could not figure out how to use chron to import this into times, so
> I tried to extract the hours and minutes on my own.
>
> I used strsplit and got a list:
>
> h2 = strsplit(h, "h")
> > h2
> [[1]]
> [1] "3" "30"
>
> [[2]]
> [1] "6" "30"
>
> [[3]]
> [1] "9" "40"
>
> [[4]]
> [1] "11" "25"
>
> [[5]]
> [1] "14" "00"
>
> [[6]]
> [1] "15" "55"
>
> [[7]]
> [1] "23"
>
> It is where I am stuck. I would have like to extract a vector of
> "hours" from this list, and a vector of "minutes", to reconstruct a
> time of day.
>
> But the only command I know, unlist, makes a long vector of h, min, h,
> min, h, min.
>
> For this in particular, but lists in general, how can one extract the
> first item of each element in the list, then the second item of each
> element, etc.?
>
> Thanks in advance,
>
> Denis
>
> ______________________________________________
> 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.
>
>

-- 
View this message in context: http://www.nabble.com/handling-the-output-of-strsplit-tp18038339p18043850.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
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.
Received on Sat 21 Jun 2008 - 12:36:11 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 23 Jun 2008 - 11:30:49 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive