Thanks for your help.
One of variable in my dataset looks like this:
> df$TM
> [1] "000054" "000020" "000056" "000051" "000025" "000116" "000219" "000207" "000233" "000206" "000142" "000126" "000237" "000215" "000236" "000246" "000219"
[18] "000227" "000803" "000920"...
The real meaning of each character is hours, minutes and seconds.
When I adjust hms function in Lubridates as follows
> df$TM <- hms(df$TM)
Warning message is coming: "In .parse_hms(..., order = "HMS", quiet = quiet) :
Some strings failed to parse, or all strings are NAs"
After that, all the values in the column changes to NA.
I also tried
> df$TM <- as.POSIXct(df$TM, format = "%H:%M:%S")
and
> df$TM <- chronicle(times = df$TM)
and
> df$TM <- strptime(df$TM, format = "%H:%M:%S")
but... these three trial also have same results.
(Actually all data has changed to NA, so warning message is same as error message to me)
I really appreciate your help.
You can make use of this answer to include a semicolon after every second element. After that you can transform the resulting character string as date (with day, month and year) or leave it as is.
For completeness, the solution for your problem then is
as.POSIXct(sub(":+$", "", gsub('(.{2})', '\\1:', df$TM)), format = "%H:%M:%S")
I am importing a csv file into R, creating a 3x3 dataframe, and attempting to convert the dataframe to an xts object. But I get error message "do not match the length of object".
#DATSB <- fread("C:/Temp/GoogleDrive/R/temp.csv", select = c("DateTime","Last","Volume"))
#that results in following dput() output:
DATSB <- structure(list(DateTime = c("3/28/2016 20:37", "3/28/2016 20:36","3/28/2016 20:35"), Last = c(1221.7, 1221.8, 1221.9), Volume = c(14L,2L, 22L)), .Names = c("DateTime", "Last", "Volume"), row.names = c(NA,3L), class = "data.frame")
setDF(DATSB)
DATSB$DateTime <- strptime(DATSB$DateTime, format = "%m/%d/%Y %H:%M")
DATSBxts <- as.xts(DATSB[, -2], order.by = as.Date(DATSB$DateTime, "%Y/%m/%d %H:%M"))
DateTime Last Volume
1 3/28/2016 20:37 1221.7 14
2 3/28/2016 20:36 1221.8 2
3 3/28/2016 20:35 1221.9 22
Exact error message is "Error in as.matrix.data.frame(x) :
dims [product 12] do not match the length of object [14]"
Somehow the root of the problem is the column Volume. Without that column, it works. Unfortunately can't figure it out. Thanks for your help!
There was a typo here DATSB[, -2], correcting it works fine. General theme for xts is,
xts(data[,-date_column], order.by = data[,date_column])
Also coredata(DATSBxts) and index(DATSBxts) are helpful functions
DATSBxts = xts(DATSB[, -1], order.by = DATSB[,1] ,dateFormat = "%Y/%m/%d %H:%M:%S");rev(DATSBxts)
DATSBxts
# Last Volume
#2016-03-28 20:35:00 1221.9 22
#2016-03-28 20:36:00 1221.8 2
#2016-03-28 20:37:00 1221.7 14
structure(list(date = c(20140717L, 20140611L, 20140611L, 20140704L,
20140411L, 20140906L, 20140512L, 20140717L, 20140819L, 20140415L,
20140812L, 20140403L, 20140424L, 20140818L, 20140922L, 20140625L,
20141006L, 20140918L, 20140811L, 20140819L, 20140602L, 20140626L,
20140729L, 20140624L, 20140909L, 20140705L, 20140920L, 20140515L,
20140531L, 20140628L, 20140822L, 20140508L, 20140809L, 20140627L,
20140727L, 20140711L, 20140714L, 20140710L, 20140403L, 20140525L,
20140428L, 20140501L, 20140915L, 20140510L, 20140601L, 20140921L,
20140815L, 20140610L, 20140418L, 20140812L, 20140614L, 20140814L,
20140626L, 20140412L, 20140912L, 20140514L, 20140919L, 20140706L,
20140411L, 20140711L, 20140624L, 20140430L, 20140521L, 20140418L,
20140713L, 20140424L, 20140601L, 20140923L, 20140406L, 20140905L,
20140613L, 20140412L, 20140407L, 20140402L, 20140813L, 20140903L,
20140827L, 20140521L, 20140524L, 20140404L, 20140419L, 20140412L,
20140902L, 20140623L, 20140925L, 20140528L, 20140731L, 20140513L,
20140821L, 20140703L, 20140724L, 20140818L, 20140801L, 20140628L,
20140801L, 20140521L, 20140906L, 20140725L, 20140522L, 20140927L,
20140615L, 20140920L, 20140813L, 20140815L, 20140924L, 20140614L,
20140912L, 20140710L, 20140807L, 20140501L, 20140420L, 20140630L,
20140704L, 20140401L, 20140605L, 20140928L, 20140806L, 20140614L,
20140907L, 20140704L, 20140403L, 20140804L, 20140603L, 20140728L,
20140919L, 20140731L, 20140426L, 20140930L, 20140502L, 20140827L,
20140815L, 20140628L, 20140902L, 20140616L, 20140613L, 20140726L,
20140721L, 20140425L, 20140715L, 20140607L, 20140913L, 20140621L,
20140708L, 20140427L, 20140506L, 20140425L, 20140411L, 20140615L,
20140713L, 20140424L, 20140406L, 20140711L, 20140415L, 20140909L,
20141004L, 20140725L, 20140602L, 20140405L, 20140525L, 20140605L,
20140521L, 20140506L, 20140414L, 20140916L, 20140512L, 20140830L,
20140722L, 20140711L, 20140628L, 20140613L, 20140618L, 20140719L,
20140416L, 20140727L, 20140521L, 20140718L, 20140814L, 20140515L,
20140501L, 20140725L, 20140507L, 20140619L, 20140525L, 20140609L,
20140614L, 20140402L, 20140914L, 20140517L, 20140826L, 20140602L,
20140920L, 20140718L, 20140915L, 20140715L, 20140708L, 20140419L,
20140819L, 20140501L, 20140807L, 20140404L)), .Names = "date", row.names = c(NA,
-200L), class = "data.frame")
This data frame has date values as class of integer. This data set is just one column of my data set. The original data set also has another variable called "total sales". I want to make a plot which has dates in X axis and on Y axis, total sales.
However, because the dates are regarded as integer, the plot is bad. So I want to let R understand the date column as date variables so I can get improved plot.
How can it be possible? Please give me help. Thank you very much.
You might have better luck with as.Date. If df is the data, then you can do
df$date <- as.Date(as.character(df$date), format = "%Y%m%d")
with(df, plot(date))
If d is the date column, you can try:
strptime(t(d),format='%Y%m%d')