Adding column names in XTS and changing dates in XTS












0















The xts looks like:



An ‘xts’ object on 1970-01-02 05:30:00/1976-03-29 05:30:00 containing:
Data: num [1:2279, 1] 0.295 0.316 0.315 0.301 0.292 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : NULL
Indexed by objects of class: [POSIXct,POSIXt] TZ:
Original class: 'double'
xts Attributes: NULL


This XTS is generated through another code and does not have column names as shown by



dimnames(cor_BG_xts)
[[1]]
NULL
[[2]]
NULL
colnames(cor_BG_xts)
NULL


How can one add column names to xts. I tried searing stack overflow but I get the solutions for data.frame instead of xts. Do, I need to first convert it to df and then name the columns.
Also, the xts takes the dates like 1970-01-02 05:30:00. How can one change the xts say 12/1/2009 12:00:00.










share|improve this question





























    0















    The xts looks like:



    An ‘xts’ object on 1970-01-02 05:30:00/1976-03-29 05:30:00 containing:
    Data: num [1:2279, 1] 0.295 0.316 0.315 0.301 0.292 ...
    - attr(*, "dimnames")=List of 2
    ..$ : NULL
    ..$ : NULL
    Indexed by objects of class: [POSIXct,POSIXt] TZ:
    Original class: 'double'
    xts Attributes: NULL


    This XTS is generated through another code and does not have column names as shown by



    dimnames(cor_BG_xts)
    [[1]]
    NULL
    [[2]]
    NULL
    colnames(cor_BG_xts)
    NULL


    How can one add column names to xts. I tried searing stack overflow but I get the solutions for data.frame instead of xts. Do, I need to first convert it to df and then name the columns.
    Also, the xts takes the dates like 1970-01-02 05:30:00. How can one change the xts say 12/1/2009 12:00:00.










    share|improve this question



























      0












      0








      0








      The xts looks like:



      An ‘xts’ object on 1970-01-02 05:30:00/1976-03-29 05:30:00 containing:
      Data: num [1:2279, 1] 0.295 0.316 0.315 0.301 0.292 ...
      - attr(*, "dimnames")=List of 2
      ..$ : NULL
      ..$ : NULL
      Indexed by objects of class: [POSIXct,POSIXt] TZ:
      Original class: 'double'
      xts Attributes: NULL


      This XTS is generated through another code and does not have column names as shown by



      dimnames(cor_BG_xts)
      [[1]]
      NULL
      [[2]]
      NULL
      colnames(cor_BG_xts)
      NULL


      How can one add column names to xts. I tried searing stack overflow but I get the solutions for data.frame instead of xts. Do, I need to first convert it to df and then name the columns.
      Also, the xts takes the dates like 1970-01-02 05:30:00. How can one change the xts say 12/1/2009 12:00:00.










      share|improve this question
















      The xts looks like:



      An ‘xts’ object on 1970-01-02 05:30:00/1976-03-29 05:30:00 containing:
      Data: num [1:2279, 1] 0.295 0.316 0.315 0.301 0.292 ...
      - attr(*, "dimnames")=List of 2
      ..$ : NULL
      ..$ : NULL
      Indexed by objects of class: [POSIXct,POSIXt] TZ:
      Original class: 'double'
      xts Attributes: NULL


      This XTS is generated through another code and does not have column names as shown by



      dimnames(cor_BG_xts)
      [[1]]
      NULL
      [[2]]
      NULL
      colnames(cor_BG_xts)
      NULL


      How can one add column names to xts. I tried searing stack overflow but I get the solutions for data.frame instead of xts. Do, I need to first convert it to df and then name the columns.
      Also, the xts takes the dates like 1970-01-02 05:30:00. How can one change the xts say 12/1/2009 12:00:00.







      r time-series xts






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 7:30









      vaettchen

      5,3451332




      5,3451332










      asked Nov 23 '18 at 7:21









      gaurav kumargaurav kumar

      3771420




      3771420
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Let's first create a reproducible example:



          library(xts)

          dates <- seq.Date(as.Date("2018-11-19"), as.Date("2018-11-23"), by = "day")
          numbers <- 1:5

          my_xts <- xts(numbers, dates)
          my_xts
          [,1]
          2018-11-19 05:30:00 1
          2018-11-20 05:30:00 2
          2018-11-21 05:30:00 3
          2018-11-22 05:30:00 4
          2018-11-23 05:30:00 5


          Now setting (renaming) a column name is not difficult, either use names, colnames, or setNames.



          names(my_xts) <- "new_column_name"
          # setNames / colnames works as well.
          # my_xts <- setNames(my_xts, "new_column_name")
          # colnames(my_xts) <- "new_column_name"
          #
          my_xts
          new_column_name
          2018-11-19 05:30:00 1
          2018-11-20 05:30:00 2
          2018-11-21 05:30:00 3
          2018-11-22 05:30:00 4
          2018-11-23 05:30:00 5


          Changing the index format, use indexFormat. You can use any date time format that is mentioned in the details of ?strptime.



          indexFormat(my_xts) <- "%d/%m/%Y %H:%M:%S"
          my_xts
          new_column_name
          19/11/2018 05:30:00 1
          20/11/2018 05:30:00 2
          21/11/2018 05:30:00 3
          22/11/2018 05:30:00 4
          23/11/2018 05:30:00 5





          share|improve this answer
























          • many thanks for the answer. My problem is that the time series is creating the time stamps (starting index from 1970) by itself. Now I have understood that it is because that I should pass the xts to the function which generates this xts object. On passing xts to the function it takes the required time stamps. Many thanks for the answer on renaming the xts columns.

            – gaurav kumar
            Nov 26 '18 at 16:10











          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%2f53442230%2fadding-column-names-in-xts-and-changing-dates-in-xts%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Let's first create a reproducible example:



          library(xts)

          dates <- seq.Date(as.Date("2018-11-19"), as.Date("2018-11-23"), by = "day")
          numbers <- 1:5

          my_xts <- xts(numbers, dates)
          my_xts
          [,1]
          2018-11-19 05:30:00 1
          2018-11-20 05:30:00 2
          2018-11-21 05:30:00 3
          2018-11-22 05:30:00 4
          2018-11-23 05:30:00 5


          Now setting (renaming) a column name is not difficult, either use names, colnames, or setNames.



          names(my_xts) <- "new_column_name"
          # setNames / colnames works as well.
          # my_xts <- setNames(my_xts, "new_column_name")
          # colnames(my_xts) <- "new_column_name"
          #
          my_xts
          new_column_name
          2018-11-19 05:30:00 1
          2018-11-20 05:30:00 2
          2018-11-21 05:30:00 3
          2018-11-22 05:30:00 4
          2018-11-23 05:30:00 5


          Changing the index format, use indexFormat. You can use any date time format that is mentioned in the details of ?strptime.



          indexFormat(my_xts) <- "%d/%m/%Y %H:%M:%S"
          my_xts
          new_column_name
          19/11/2018 05:30:00 1
          20/11/2018 05:30:00 2
          21/11/2018 05:30:00 3
          22/11/2018 05:30:00 4
          23/11/2018 05:30:00 5





          share|improve this answer
























          • many thanks for the answer. My problem is that the time series is creating the time stamps (starting index from 1970) by itself. Now I have understood that it is because that I should pass the xts to the function which generates this xts object. On passing xts to the function it takes the required time stamps. Many thanks for the answer on renaming the xts columns.

            – gaurav kumar
            Nov 26 '18 at 16:10
















          0














          Let's first create a reproducible example:



          library(xts)

          dates <- seq.Date(as.Date("2018-11-19"), as.Date("2018-11-23"), by = "day")
          numbers <- 1:5

          my_xts <- xts(numbers, dates)
          my_xts
          [,1]
          2018-11-19 05:30:00 1
          2018-11-20 05:30:00 2
          2018-11-21 05:30:00 3
          2018-11-22 05:30:00 4
          2018-11-23 05:30:00 5


          Now setting (renaming) a column name is not difficult, either use names, colnames, or setNames.



          names(my_xts) <- "new_column_name"
          # setNames / colnames works as well.
          # my_xts <- setNames(my_xts, "new_column_name")
          # colnames(my_xts) <- "new_column_name"
          #
          my_xts
          new_column_name
          2018-11-19 05:30:00 1
          2018-11-20 05:30:00 2
          2018-11-21 05:30:00 3
          2018-11-22 05:30:00 4
          2018-11-23 05:30:00 5


          Changing the index format, use indexFormat. You can use any date time format that is mentioned in the details of ?strptime.



          indexFormat(my_xts) <- "%d/%m/%Y %H:%M:%S"
          my_xts
          new_column_name
          19/11/2018 05:30:00 1
          20/11/2018 05:30:00 2
          21/11/2018 05:30:00 3
          22/11/2018 05:30:00 4
          23/11/2018 05:30:00 5





          share|improve this answer
























          • many thanks for the answer. My problem is that the time series is creating the time stamps (starting index from 1970) by itself. Now I have understood that it is because that I should pass the xts to the function which generates this xts object. On passing xts to the function it takes the required time stamps. Many thanks for the answer on renaming the xts columns.

            – gaurav kumar
            Nov 26 '18 at 16:10














          0












          0








          0







          Let's first create a reproducible example:



          library(xts)

          dates <- seq.Date(as.Date("2018-11-19"), as.Date("2018-11-23"), by = "day")
          numbers <- 1:5

          my_xts <- xts(numbers, dates)
          my_xts
          [,1]
          2018-11-19 05:30:00 1
          2018-11-20 05:30:00 2
          2018-11-21 05:30:00 3
          2018-11-22 05:30:00 4
          2018-11-23 05:30:00 5


          Now setting (renaming) a column name is not difficult, either use names, colnames, or setNames.



          names(my_xts) <- "new_column_name"
          # setNames / colnames works as well.
          # my_xts <- setNames(my_xts, "new_column_name")
          # colnames(my_xts) <- "new_column_name"
          #
          my_xts
          new_column_name
          2018-11-19 05:30:00 1
          2018-11-20 05:30:00 2
          2018-11-21 05:30:00 3
          2018-11-22 05:30:00 4
          2018-11-23 05:30:00 5


          Changing the index format, use indexFormat. You can use any date time format that is mentioned in the details of ?strptime.



          indexFormat(my_xts) <- "%d/%m/%Y %H:%M:%S"
          my_xts
          new_column_name
          19/11/2018 05:30:00 1
          20/11/2018 05:30:00 2
          21/11/2018 05:30:00 3
          22/11/2018 05:30:00 4
          23/11/2018 05:30:00 5





          share|improve this answer













          Let's first create a reproducible example:



          library(xts)

          dates <- seq.Date(as.Date("2018-11-19"), as.Date("2018-11-23"), by = "day")
          numbers <- 1:5

          my_xts <- xts(numbers, dates)
          my_xts
          [,1]
          2018-11-19 05:30:00 1
          2018-11-20 05:30:00 2
          2018-11-21 05:30:00 3
          2018-11-22 05:30:00 4
          2018-11-23 05:30:00 5


          Now setting (renaming) a column name is not difficult, either use names, colnames, or setNames.



          names(my_xts) <- "new_column_name"
          # setNames / colnames works as well.
          # my_xts <- setNames(my_xts, "new_column_name")
          # colnames(my_xts) <- "new_column_name"
          #
          my_xts
          new_column_name
          2018-11-19 05:30:00 1
          2018-11-20 05:30:00 2
          2018-11-21 05:30:00 3
          2018-11-22 05:30:00 4
          2018-11-23 05:30:00 5


          Changing the index format, use indexFormat. You can use any date time format that is mentioned in the details of ?strptime.



          indexFormat(my_xts) <- "%d/%m/%Y %H:%M:%S"
          my_xts
          new_column_name
          19/11/2018 05:30:00 1
          20/11/2018 05:30:00 2
          21/11/2018 05:30:00 3
          22/11/2018 05:30:00 4
          23/11/2018 05:30:00 5






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 11:05









          phiverphiver

          13.8k92936




          13.8k92936













          • many thanks for the answer. My problem is that the time series is creating the time stamps (starting index from 1970) by itself. Now I have understood that it is because that I should pass the xts to the function which generates this xts object. On passing xts to the function it takes the required time stamps. Many thanks for the answer on renaming the xts columns.

            – gaurav kumar
            Nov 26 '18 at 16:10



















          • many thanks for the answer. My problem is that the time series is creating the time stamps (starting index from 1970) by itself. Now I have understood that it is because that I should pass the xts to the function which generates this xts object. On passing xts to the function it takes the required time stamps. Many thanks for the answer on renaming the xts columns.

            – gaurav kumar
            Nov 26 '18 at 16:10

















          many thanks for the answer. My problem is that the time series is creating the time stamps (starting index from 1970) by itself. Now I have understood that it is because that I should pass the xts to the function which generates this xts object. On passing xts to the function it takes the required time stamps. Many thanks for the answer on renaming the xts columns.

          – gaurav kumar
          Nov 26 '18 at 16:10





          many thanks for the answer. My problem is that the time series is creating the time stamps (starting index from 1970) by itself. Now I have understood that it is because that I should pass the xts to the function which generates this xts object. On passing xts to the function it takes the required time stamps. Many thanks for the answer on renaming the xts columns.

          – gaurav kumar
          Nov 26 '18 at 16:10




















          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%2f53442230%2fadding-column-names-in-xts-and-changing-dates-in-xts%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

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          RAC Tourist Trophy