[R] Adding Time when Blanks
Rui Barradas
ruipbarradas at sapo.pt
Fri Oct 12 20:35:32 CEST 2012
Hello,
Try the following.
dat <- read.table(text="
ID group Start_date Time_of_experiment Time_of_end
1 20209 4 02/02/2009 12:38:00
26 30209 3 03/02/2009 12:40:00 13:32:00
27 31609 4 03/16/2009 11:28:00 12:26:00
28 40609 4 04/06/2009 11:17:00
53 42709 4 04/27/2009 11:15:00 9:30:00
76 51109 3 05/11/2009 11:51:00
101 51809 1 05/18/2009 12:28:00
126 62209 3 06/22/2009 11:31:00
150 71309 4 07/13/2009 12:12:00 13:37:00
173 81009 4 08/10/2009 11:32:00 20:52:00
", header=TRUE, fill=TRUE)
str(dat)
dat$start_time <- with(dat, paste(Start_date, Time_of_experiment))
dat$Start_of_Experiment <-
as.POSIXct(strptime(dat$start_time, "%m/%d/%Y %H:%M:%S"))
#--- Create End_of_Experiment
idx <- dat$Time_of_end != ''
dat$End_of_Experiment <- dat$Start_of_Experiment + 48*60*60
dat$End_of_Experiment[idx] <-
as.POSIXct(strptime(paste(dat$Start_date, dat$Time_of_end)[idx],
"%m/%d/%Y %H:%M:%S"))
dat
Hope this helps,
Rui Barradas
Em 12-10-2012 18:59, Charles Determan Jr escreveu:
> Greetings,
>
> My data set has dates and times that I am working with. Some of the times
> in Time_of_end are blank. This is supposed to dictate that the particular
> experiment lasted 48 hours. I would like to add 48 hours to the start
> Start_of_Experiment for another column as End_of_Experiment including both
> the ones with 48 added and those with early times. I was thinking
> something with a conditional statement but I can't seem to figure out what
> code to use. Any insight would be appreciated. Let me know if there is
> anything else you need. Thanks for your time.
>
> I have Start_of_Experiment in POSIX format for time calculations from the
> following:
>
> data$Start_of_Experiment=as.POSIXct(strptime(data$start_time, "%m/%d/%Y
> %H:%M:%S"))
>
> Here is a subset of my data.
>
> ID group Start_date Time_of_experiment Time_of_end
> 1 20209 4 02/02/2009 12:38:00
> 26 30209 3 03/02/2009 12:40:00 13:32:00
> 27 31609 4 03/16/2009 11:28:00 12:26:00
> 28 40609 4 04/06/2009 11:17:00
> 53 42709 4 04/27/2009 11:15:00 9:30:00
> 76 51109 3 05/11/2009 11:51:00
> 101 51809 1 05/18/2009 12:28:00
> 126 62209 3 06/22/2009 11:31:00
> 150 71309 4 07/13/2009 12:12:00 13:37:00
> 173 81009 4 08/10/2009 11:32:00 20:52:00
> Start_of_Experiment
> 1 2009-02-02 12:38:00
> 26 2009-03-02 12:40:00
> 27 2009-03-16 11:28:00
> 28 2009-04-06 11:17:00
> 53 2009-04-27 11:15:00
> 76 2009-05-11 11:51:00
> 101 2009-05-18 12:28:00
> 126 2009-06-22 11:31:00
> 150 2009-07-13 12:12:00
> 173 2009-08-10 11:32:00
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
More information about the R-help
mailing list