different formats of dates using as.POSIXct() or similar












0















I am writing a function where I plot several different data frames composed by time series.
The data frames are composed by several columns. The first one of each is always "time", and the following are the parameters.
Any data frame is different from each other



Once I imported the data set, the function create a calendar vector "Time"



Time <- as.POSIXct(TS[,1])


In a for loop, I use the function xts() to create the time series of one single parameter (one by one), using the column "time" to order the time series.



xts(TS[,i],order.by = Time)


then I plot.



As result, the script looks like this



TS <- read.table("ts.txt",header = T, dec = ".")
Time <- as.POSIXct(TS[,1])

for (i in 2:length(TS[1,])
{
p <- plot(xts(TS[,i],order.by = Time)
print(p)
}


I have problems with as.POSIXct() when the format of my vector time in the data frames is not yyyy-mm-dd. Here few examples:



In some data frames, in "time" I have only "yyyy" in which pasting the "mm-dd" to "yyyy", would not make any sense because of the data (the columns, in this case, are the months).



In other situations, I have also negative dates because they are BC.



Are there other functions I can use to create calendar dates suitable to xts() using a different format like mine?



Here three examples of data set I have the problem with:



#1
year <- c(seq(1900, 2000, by = 10))
Jan <- c(rnorm(length(year), mean = 1, 5))
Feb <- c(rnorm(length(year), mean = 6, 9))
TS <- as.data.frame(cbind(year,Jan,Feb))
str(TS)

#2
year <- c(seq(-500, 2000, by = 100))
Jan <- c(rnorm(length(year), mean = 1, 5))
Feb <- c(rnorm(length(year), mean = 6, 9))
TS <- as.data.frame(cbind(year,Jan,Feb))
str(TS)

#3
time <- c("-100/01/01", "-100/06/01", "0/01/01", "1400/01/01", "2000/01/01")
people <- abs(c(rnorm(length(time), mean = 6, 9)))
TS <- as.data.frame(cbind(time,people))
str(TS)









share|improve this question




















  • 3





    "Parsing dates" is an impossibly broad issue, can you please provide some more specific example input data that you're working with

    – MichaelChirico
    Nov 21 '18 at 8:38











  • Thanks Micheal, Here you can find a few examples as you required

    – Strobila
    Nov 25 '18 at 9:02
















0















I am writing a function where I plot several different data frames composed by time series.
The data frames are composed by several columns. The first one of each is always "time", and the following are the parameters.
Any data frame is different from each other



Once I imported the data set, the function create a calendar vector "Time"



Time <- as.POSIXct(TS[,1])


In a for loop, I use the function xts() to create the time series of one single parameter (one by one), using the column "time" to order the time series.



xts(TS[,i],order.by = Time)


then I plot.



As result, the script looks like this



TS <- read.table("ts.txt",header = T, dec = ".")
Time <- as.POSIXct(TS[,1])

for (i in 2:length(TS[1,])
{
p <- plot(xts(TS[,i],order.by = Time)
print(p)
}


I have problems with as.POSIXct() when the format of my vector time in the data frames is not yyyy-mm-dd. Here few examples:



In some data frames, in "time" I have only "yyyy" in which pasting the "mm-dd" to "yyyy", would not make any sense because of the data (the columns, in this case, are the months).



In other situations, I have also negative dates because they are BC.



Are there other functions I can use to create calendar dates suitable to xts() using a different format like mine?



Here three examples of data set I have the problem with:



#1
year <- c(seq(1900, 2000, by = 10))
Jan <- c(rnorm(length(year), mean = 1, 5))
Feb <- c(rnorm(length(year), mean = 6, 9))
TS <- as.data.frame(cbind(year,Jan,Feb))
str(TS)

#2
year <- c(seq(-500, 2000, by = 100))
Jan <- c(rnorm(length(year), mean = 1, 5))
Feb <- c(rnorm(length(year), mean = 6, 9))
TS <- as.data.frame(cbind(year,Jan,Feb))
str(TS)

#3
time <- c("-100/01/01", "-100/06/01", "0/01/01", "1400/01/01", "2000/01/01")
people <- abs(c(rnorm(length(time), mean = 6, 9)))
TS <- as.data.frame(cbind(time,people))
str(TS)









share|improve this question




















  • 3





    "Parsing dates" is an impossibly broad issue, can you please provide some more specific example input data that you're working with

    – MichaelChirico
    Nov 21 '18 at 8:38











  • Thanks Micheal, Here you can find a few examples as you required

    – Strobila
    Nov 25 '18 at 9:02














0












0








0








I am writing a function where I plot several different data frames composed by time series.
The data frames are composed by several columns. The first one of each is always "time", and the following are the parameters.
Any data frame is different from each other



Once I imported the data set, the function create a calendar vector "Time"



Time <- as.POSIXct(TS[,1])


In a for loop, I use the function xts() to create the time series of one single parameter (one by one), using the column "time" to order the time series.



xts(TS[,i],order.by = Time)


then I plot.



As result, the script looks like this



TS <- read.table("ts.txt",header = T, dec = ".")
Time <- as.POSIXct(TS[,1])

for (i in 2:length(TS[1,])
{
p <- plot(xts(TS[,i],order.by = Time)
print(p)
}


I have problems with as.POSIXct() when the format of my vector time in the data frames is not yyyy-mm-dd. Here few examples:



In some data frames, in "time" I have only "yyyy" in which pasting the "mm-dd" to "yyyy", would not make any sense because of the data (the columns, in this case, are the months).



In other situations, I have also negative dates because they are BC.



Are there other functions I can use to create calendar dates suitable to xts() using a different format like mine?



Here three examples of data set I have the problem with:



#1
year <- c(seq(1900, 2000, by = 10))
Jan <- c(rnorm(length(year), mean = 1, 5))
Feb <- c(rnorm(length(year), mean = 6, 9))
TS <- as.data.frame(cbind(year,Jan,Feb))
str(TS)

#2
year <- c(seq(-500, 2000, by = 100))
Jan <- c(rnorm(length(year), mean = 1, 5))
Feb <- c(rnorm(length(year), mean = 6, 9))
TS <- as.data.frame(cbind(year,Jan,Feb))
str(TS)

#3
time <- c("-100/01/01", "-100/06/01", "0/01/01", "1400/01/01", "2000/01/01")
people <- abs(c(rnorm(length(time), mean = 6, 9)))
TS <- as.data.frame(cbind(time,people))
str(TS)









share|improve this question
















I am writing a function where I plot several different data frames composed by time series.
The data frames are composed by several columns. The first one of each is always "time", and the following are the parameters.
Any data frame is different from each other



Once I imported the data set, the function create a calendar vector "Time"



Time <- as.POSIXct(TS[,1])


In a for loop, I use the function xts() to create the time series of one single parameter (one by one), using the column "time" to order the time series.



xts(TS[,i],order.by = Time)


then I plot.



As result, the script looks like this



TS <- read.table("ts.txt",header = T, dec = ".")
Time <- as.POSIXct(TS[,1])

for (i in 2:length(TS[1,])
{
p <- plot(xts(TS[,i],order.by = Time)
print(p)
}


I have problems with as.POSIXct() when the format of my vector time in the data frames is not yyyy-mm-dd. Here few examples:



In some data frames, in "time" I have only "yyyy" in which pasting the "mm-dd" to "yyyy", would not make any sense because of the data (the columns, in this case, are the months).



In other situations, I have also negative dates because they are BC.



Are there other functions I can use to create calendar dates suitable to xts() using a different format like mine?



Here three examples of data set I have the problem with:



#1
year <- c(seq(1900, 2000, by = 10))
Jan <- c(rnorm(length(year), mean = 1, 5))
Feb <- c(rnorm(length(year), mean = 6, 9))
TS <- as.data.frame(cbind(year,Jan,Feb))
str(TS)

#2
year <- c(seq(-500, 2000, by = 100))
Jan <- c(rnorm(length(year), mean = 1, 5))
Feb <- c(rnorm(length(year), mean = 6, 9))
TS <- as.data.frame(cbind(year,Jan,Feb))
str(TS)

#3
time <- c("-100/01/01", "-100/06/01", "0/01/01", "1400/01/01", "2000/01/01")
people <- abs(c(rnorm(length(time), mean = 6, 9)))
TS <- as.data.frame(cbind(time,people))
str(TS)






r xts






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 8:48







Strobila

















asked Nov 21 '18 at 8:31









StrobilaStrobila

3418




3418








  • 3





    "Parsing dates" is an impossibly broad issue, can you please provide some more specific example input data that you're working with

    – MichaelChirico
    Nov 21 '18 at 8:38











  • Thanks Micheal, Here you can find a few examples as you required

    – Strobila
    Nov 25 '18 at 9:02














  • 3





    "Parsing dates" is an impossibly broad issue, can you please provide some more specific example input data that you're working with

    – MichaelChirico
    Nov 21 '18 at 8:38











  • Thanks Micheal, Here you can find a few examples as you required

    – Strobila
    Nov 25 '18 at 9:02








3




3





"Parsing dates" is an impossibly broad issue, can you please provide some more specific example input data that you're working with

– MichaelChirico
Nov 21 '18 at 8:38





"Parsing dates" is an impossibly broad issue, can you please provide some more specific example input data that you're working with

– MichaelChirico
Nov 21 '18 at 8:38













Thanks Micheal, Here you can find a few examples as you required

– Strobila
Nov 25 '18 at 9:02





Thanks Micheal, Here you can find a few examples as you required

– Strobila
Nov 25 '18 at 9:02












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407960%2fdifferent-formats-of-dates-using-as-posixct-or-similar%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407960%2fdifferent-formats-of-dates-using-as-posixct-or-similar%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents