Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7930521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:27:15+00:00 2026-06-03T20:27:15+00:00

I regularly build a data frame with time values (originating from xts or zoo

  • 0

I regularly build a data frame with time values (originating from xts or zoo objects) for analysis and plotting. Here’s an example:

library(xts)   

x1 <- xts(rnorm(10), as.Date("2012-01-01") + 0:9)   
x2 <- xts(rnorm(10), as.Date("2012-01-04") + 0:9)   

(df <- data.frame(merge(x1=x1, x2=x2), v1=4:16, v2=rnorm(13)))    

                   x1          x2 v1          v2   
2012-01-01  0.1930827          NA  4  1.05972724   
2012-01-02  0.4429592          NA  5 -1.89299068   
2012-01-03  1.6657630          NA  6  0.70445966  
2012-01-04 -0.2765922 -0.26728223  7  0.35336959   
2012-01-05 -0.1756590 -2.04888130  8  0.90129924  
2012-01-06 -2.4849132  0.49400975  9  1.54486914  
2012-01-07 -0.9993353 -1.09308203 10  1.16600015  
2012-01-08 -0.7326309  0.55781566 11  0.37178542  
2012-01-09 -0.2973543 -0.59872496 12  0.07512468  
2012-01-10 -1.5061380  0.08567125 13  1.77494367  
2012-01-11         NA  0.81835375 14 -0.38211167  
2012-01-12         NA  1.30131894 15 -1.09220795  
2012-01-13         NA -1.29505649 16  1.27148069  

To plot the time series, I need to use as.Date(row.names(df)) for the horizontal axis. For example:

plot(as.Date(row.names(df)), df$x1, type="l", xlab="", ylab="Test", main="Using row.names()", col="red")   
lines(as.Date(row.names(df)), df$x2, col="blue")    
lines(as.Date(row.names(df)), df$v2, col="green")    

It’s a real pain to use as.Date(row.names(df)) over and over, so in the past, I added a column to the data frame using the following code:

(df <- cbind(df, tim=as.Date(row.names(df))))    

I can now plot the time series as:

with(df, {  
  plot(tim, x1, type="l", xlab="", ylab="Test", main="Using a new tim column", col="red")  
  lines(tim, x2, col="blue")  
  lines(tim, v2, col="green")  
})

This technique works, but adding that tim column feels sloppy. Is there a better/cleaner way to do this?

Edit 1 (2012-05-13) ====================================

The reason I’m using a data frame is because this data is commonly fed to one or more packages that require a data frame, and/or because I typically need to represent non-numeric data in some of the variables.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T20:27:16+00:00Added an answer on June 3, 2026 at 8:27 pm

    I would stick with the zoo facilities as much as possible:

    library(xts)   
    x1 <- xts(rnorm(10), as.Date("2012-01-01") + 0:9)   
    x2 <- xts(rnorm(10), as.Date("2012-01-04") + 0:9)   
    
    dz <- merge(x1=x1, x2=x2); dz$v1=4:16; dz$v2=rnorm(13)    
     # keeps it a zoo object rather than a data.frame
    
    with(dz, plot( x1, type="l", xlab="", ylab="Test", main="Not using row.names()\nStick with zoo" ) )
    lines( dz$x2, col="blue")    
    lines( dz$v2, col="green")
    

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to build a string from the data contained in this url using
I have a class that is called regularly by several objects. I would like
I'm writing a GUI application that regularly retrieves data through a web connection. Since
I have a database which I regularly need to import large amounts of data
After thinking for long, I have decided to build my data app for the
I have data from a ResultSet that contains three fields: DataType, Data, DataValue. I
This is my first time playing around with SignalR. I am trying to build
Background: I'm writing a data provider that SSAS will use to get data from
Is it possible/practical to build a single regular expression that matches hierarchical data? For
I've got a table in a database that stores log data by time. For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.