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

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.? >sapply(h2, `[`, 1) [1] "3" "6" "9" etc. sapply(h2, `[`, 2) [1] "30" "30" "40" etc. ? `[` vQ

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.? >you can also want to convert this representation to something cron-readable: times(gsub("([0-9]+)h([0-9]*)", "\\1:0\\2:0", h)) [1] 03:30:00 06:30:00 etc. (the first 0 in the replacement accommodates for missing minutes digits, the second for seconds) vQ

We construct a times object by replacing the letter h with a : and then pasting a :00 on the end. Then replace any occurrence of :: with :00: . Its now in the format that times recognizes so we can just convert that to times and apply hours() and minutes() to get the components:> library(chron) > h2 <- times(sub("::", ":00:", paste(sub("h", ":", h), "00", sep = ":"))) > hours(h2)[1] 3 6 9 11 14 15 23> minutes(h2)[1] 30 30 40 25 0 55 0\ Another possibility is to use gsubfn in package gsubfn. It matches the string such that it captures the hour and minutes in the two backreferences and then pastes them together with a :00 at the end. It then replaces :: with :00: and converts that to times. hours() and minutes() could be used, as before, to get the components.> library(gsubfn) > times(gsubfn("([^h]+)h(.*)", ~ sub("::", ":00:", paste(..., "00", sep = ":")), h, backref = -2))[1] 03:30:00 06:30:00 09:40:00 11:25:00 14:00:00 15:55:00 23:00:00 Here is another approach using strapply in the gsubfn package. We use the same pattern but this time convert each component to numeric:> times(strapply(h, "([^h]+)h(.*)", ~ as.numeric(x) / 24 + sum(as.numeric(y), na.rm = TRUE)/(24*60), backref = -2, simplify = c))[1] 03:30:00 06:30:00 09:40:00 11:25:00 14:00:00 15:55:00 23:00:00 On Fri, Jun 20, 2008 at 6:14 PM, Denis Chabot <chabotd at globetrotter.net> 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. >

Most helpful Gabor, Many thanks, Denis Le 08-06-20 ? 18:58, Gabor Grothendieck a ?crit :> We construct a times object by replacing the letter h with > a : and then pasting a :00 on the end. Then replace any occurrence > of :: with :00: . Its now in the format that times recognizes so we > can > just convert that to times and apply hours() and minutes() to get > the components: > >> library(chron) >> h2 <- times(sub("::", ":00:", paste(sub("h", ":", h), "00", sep = >> ":"))) >> hours(h2) > [1] 3 6 9 11 14 15 23 >> minutes(h2) > [1] 30 30 40 25 0 55 0\ > > Another possibility is to use gsubfn in package gsubfn. It matches > the > string such that it captures the hour and minutes in the two > backreferences > and then pastes them together with a :00 at the end. It then > replaces > :: with :00: and converts that to times. hours() and minutes() > could be used, > as before, to get the components. > >> library(gsubfn) >> times(gsubfn("([^h]+)h(.*)", ~ sub("::", ":00:", paste(..., "00", >> sep = ":")), h, backref = -2)) > [1] 03:30:00 06:30:00 09:40:00 11:25:00 14:00:00 15:55:00 23:00:00 > > Here is another approach using strapply in the gsubfn package. We > use the > same pattern but this time convert each component to numeric: > >> times(strapply(h, "([^h]+)h(.*)", ~ as.numeric(x) / 24 + >> sum(as.numeric(y), na.rm = TRUE)/(24*60), backref = -2, simplify = >> c)) > [1] 03:30:00 06:30:00 09:40:00 11:25:00 14:00:00 15:55:00 23:00:00 > > > > On Fri, Jun 20, 2008 at 6:14 PM, Denis Chabot <chabotd at globetrotter.net > > 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. >>

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.

On Sat, 21-Jun-2008 at 05:33AM -0700, Mark Difford wrote: |> |> 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" That''s a neat approach, however, it won''t work for this one:> strptime("14h00", format="%Ih%M")[1] NA But this will:> strptime("14h00", format="%Hh%M")[1] "2008-06-23 14:00:00" However (again), *it* also won''t work here:> strptime("23h", format="%Hh%M")[1] NA>So it''s back to the strsplit method for it unless you can get a zero after the ''h'', and the best way of doing that would depend on how your real data is being entered. -- ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~. ___ Patrick Connolly {~._.~} Great minds discuss ideas _( Y )_ Middle minds discuss events (:_~*~_:) Small minds discuss people (_)-(_) ..... Anon ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.

One way, if you are unsure of your data, is to append two additional zeros on to the strings:> strptime("23h", format="%Hh%M")[1] NA> strptime(paste("23h", "00", sep=""), format="%Hh%M")[1] "2008-06-23 23:00:00 GMT"> strptime(paste("23h04", "00", sep=""), format="%Hh%M")[1] "2008-06-23 23:04:00 GMT">On Mon, Jun 23, 2008 at 6:29 AM, Patrick Connolly <p_connolly at slingshot.co.nz> wrote:> On Sat, 21-Jun-2008 at 05:33AM -0700, Mark Difford wrote: > > |> > |> 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" > > That''s a neat approach, however, it won''t work for this one: >> strptime("14h00", format="%Ih%M") > [1] NA > > But this will: >> strptime("14h00", format="%Hh%M") > [1] "2008-06-23 14:00:00" > > However (again), *it* also won''t work here: > >> strptime("23h", format="%Hh%M") > [1] NA >> > > So it''s back to the strsplit method for it unless you can get a zero > after the ''h'', and the best way of doing that would depend on how your > real data is being entered. > > > -- > ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~. > ___ Patrick Connolly > {~._.~} Great minds discuss ideas > _( Y )_ Middle minds discuss events > (:_~*~_:) Small minds discuss people > (_)-(_) ..... Anon > > ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~. > > ______________________________________________ > 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 you are trying to solve?