datetime - Unexpected results using strptime in R -


last year, used code below convert character string datetime , worked, unexpected results after running strptime.

data <- structure(list(time = c("12:00 am", "1:00 am", "2:00 am",   "3:00 am", "4:00 am", "5:00 am", "6:00 am", "7:00 am", "8:00 am",   "9:00 am", "10:00 am", "11:00 am")),   .names = "time", class = "data.frame", row.names = c(na, -12l)) 

why give me expected results strptime:

strptime(data$time[1:10], format="%l:%m %p") #  [1] "2013-03-01 00:00:00" "2013-03-01 01:00:00" "2013-03-01 02:00:00" #  [4] "2013-03-01 03:00:00" "2013-03-01 04:00:00" "2013-03-01 05:00:00" #  [7] "2013-03-01 06:00:00" "2013-03-01 07:00:00" "2013-03-01 08:00:00" # [10] "2013-03-01 09:00:00" 

but when try replace existing data new data format, warning , unexpected results below:

data$time[1:10] <- strptime(data$time[1:10], format="%l:%m %p") # warning message: # in data$time[1:10] <- strptime(data$time[1:10], format = "%l:%m %p") : #   number of items replace not multiple of replacement length data #                                                time # 1                      0, 0, 0, 0, 0, 0, 0, 0, 0, 0 # 2                      0, 0, 0, 0, 0, 0, 0, 0, 0, 0 # 3                      0, 1, 2, 3, 4, 5, 6, 7, 8, 9 # 4                      1, 1, 1, 1, 1, 1, 1, 1, 1, 1 # 5                      2, 2, 2, 2, 2, 2, 2, 2, 2, 2 # 6  113, 113, 113, 113, 113, 113, 113, 113, 113, 113 # 7                      5, 5, 5, 5, 5, 5, 5, 5, 5, 5 # 8            59, 59, 59, 59, 59, 59, 59, 59, 59, 59 # 9                      0, 0, 0, 0, 0, 0, 0, 0, 0, 0 # 10                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0 # 11                                         10:00 # 12                                         11:00 

when run code data$time[1:10] <- strptime(data$time[1:10], format="%l:%m %p") in question above , view(data) resulting data in standard rgui looks expected, when view(data) in rstudio unexpected results above displayed. in both rgui , rstudio function class(data$time), returns [1] "posixlt" "posixt" , behaves expected. appears problem displaying data in rstudio.

i don't know how have worked in past, unless supported in earlier version of r...

strptime returns posixlt object, list several components (see ?posixlt description of elements). problem arises because try replace portion of character vector posixlt object. converts entire column list, first 10 elements components of posixlt object, , last 2 original elements character vector.

everything work fine if replace entire column result strptime or make new column result strptime:

data$time2 <- strptime(data$time, format="%l:%m %p") data$time  <- strptime(data$time, format="%l:%m %p") 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -