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 297237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:37:17+00:00 2026-05-12T06:37:17+00:00

I have a data frame containing multiple time series of returns, stored in columns.

  • 0

I have a data frame containing multiple time series of returns, stored in columns.

The first column contains dates, and subsequent columns are independent time series each with a name. The column headers are the variable names.

## I have a data frame like this
t <- seq(as.Date('2009-01-01'),by='days',length=10)
X <- rnorm(10,0,1)
Y <- rnorm(10,0,2)
Z <- rnorm(10,0,4)

dat <- data.frame(t,X,Y,Z)

## which appears as
           t          X          Y         Z
1 2009-01-01 -1.8763317 -0.1885183 -6.655663
2 2009-01-02 -1.3566227 -2.1851226 -3.863576
3 2009-01-03 -1.3447188  2.4180249 -1.543931

I want to plot each time series as a line on a separate plot, in a lattice, with each plot labeled by the variable names.

To plot this with lattice, the data must be in a tall format, as such:

           t symbol       price
1 2009-01-01      X -1.8763317
2 2009-01-02      Y -0.1885183
2 2009-01-02      Z -6.655663

What is a good function call to do this?

  • 1 1 Answer
  • 3 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-05-12T06:37:17+00:00Added an answer on May 12, 2026 at 6:37 am

    you can also use melt() from the ‘reshape’ library (I think it’s easier to use than reshape() itself) – that’ll save you the extra step of having to add the time column back in…

    > library(reshape)
    > m <- melt(dat,id="t",variable_name="symbol")
    > names(m) <- sub("value","price",names(m))
    > head(m)
               t symbol       price
    1 2009-01-01      X -1.14945096
    2 2009-01-02      X -0.07619870
    3 2009-01-03      X  0.01547395
    4 2009-01-04      X -0.31493143
    5 2009-01-05      X  1.26985167
    6 2009-01-06      X  1.31492397
    > class(m$t)
    [1] "Date"
    > library(lattice)                                                              
    > xyplot( price ~ t | symbol, data=m ,type ="l", layout = c(1,3) )
    

    For this particular task, however, I would consider using the ‘zoo’ library, which would not require you to reshape the data frame:

    > library(zoo)                                                                  
    > zobj <- zoo(dat[,-1],dat[,1])                                                 
    > plot(zobj,col=rainbow(ncol(zobj))) 
    

    R developers/contributors (Gabor and Hadley in this case) have blessed us with many great choices. (and can’t forget Deepayan for the lattice package)

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

Sidebar

Related Questions

I have a data.frame of a time series of data, I would like to
I have a data frame containing 10k rows, for a given column X I
I have a data frame with several factor columns containing NaN 's that I
In a data frame, I have one column containing character strings. Let's say it
I have a file where a data structure containing 6 columns is stored side
I have a data frame with start dates and end dates, along with the
I have a data frame that is some 35,000 rows, by 7 columns. it
I have a data frame consisting of results from multiple runs of an experiment,
I have a data frame of daily data: df with four columns: Date, A,
I have a data frame containing the following fields: a, b, c. a and

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.