?ifelse

> t2 <- ifelse(nchar(times)<4, paste(" ", times, sep=""), times)
> strptime(t2, "%H%M")

Nikhil


On Thu, Jun 3, 2010 at 5:21 PM, Peter Moore <pmo...@iastate.edu> wrote:

> Hi,
> I'm newish to R, a recent convert from Matlab... So far I'm impressed, and
> determined to solve the following problem, which seems like it should be
> easy:
> I have a long (millions of points) data series recorded with a datalogger
> that produced a timestamp in 4 columns: Year, Day of Year, Time in (H)HMM
> and Seconds.  I would like to have R interpret these columns as a time
> object and have made some progress (e.g., using paste() to create a single
> column and then strptime() to interpret -- is that too roundabout??), but
> one thing is throwing me off and I can't seem to conquer it.  The
> hour-minute column in the raw data has no colon, so noon looks like "1200".
> Morning times have only 3 characters and afternoon times have 4.  I've been
> playing around with a fake set of times:
>   times <- c(110, 230, 459, 1001, 1238, 1922)
>
> When I use
>   strptime(data, "%k%M")
> the last three are interpreted fine but the first three are messed up
> because, for some reason, (even though I use %k for hour format?) the first
> two characters are assumed to be hour and the remaining one is minutes.
>  For
> times[3] I get NA because R doesn't know what to do with 45 hours...
>   [1] "2010-06-03 11:00:00" "2010-06-03 23:00:00" NA
>   [4] "2010-06-03 10:01:00" "2010-06-03 12:38:00" "2010-06-03 19:22:00"
>
> Fair enough, so I tried a different angle, using an if...else statement:
>   hours <- if(nchar(times)>3) strtrim(times,2) else strtrim(times,1)
>
> This worked great when times was only a vector of length=1, but when I try
> to apply it to something larger, I get the following warning:
>   Warning message:
>     In  if(nchar(times)>3) strtrim(times,2) else strtrim(times,1)  :
>     the condition has length > 1 and only the first element will be used
> and the output hours are only the first character.  Not entirely sure if I
> understand this.
>
> Any advice on how to do this?  Are there packages or commands that I'm not
> aware of that know how to deal with (h)hmm times?
>
> Thanks much,
> -Pete
> ---------------------------------------------
> platform       i486-pc-linux-gnu
> arch           i486
> os             linux-gnu
> system         i486, linux-gnu
> status
> major          2
> minor          10.1
> year           2009
> month          12
> day            14
> svn rev        50720
> language       R
> version.string R version 2.10.1 (2009-12-14)
>
> --
> Pete Moore
> Postdoctoral Research Associate
> Dept. Geological & Atmospheric Sciences
> Iowa State University
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@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.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to