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

  • Home
  • SEARCH
  • 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 6693465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:59:08+00:00 2026-05-26T05:59:08+00:00

I am working with data, 1st two columns are dates, 3rd column is symbol,

  • 0

I am working with data, 1st two columns are dates, 3rd column is symbol, and 4th and 5th columns are prices.
So, I created a subset of the data as follows:

test.sub<-subset(test,V3=="GOOG",select=c(V1,V4)

and then I try to plot a time series chart using the following

as.ts(test.sub)
plot(test.sub)

well, it gives me a scatter plot – not what I was looking for.
so, I tried plot(test.sub[1],test.sub[2])
and now I get the following error:

Error in xy.coords(x, y, xlabel, ylabel, log) : 
  'x' and 'y' lengths differ

To make sure the no. of rows were same, I ran nrow(test.sub[1]) and nrow(test.sub[2]) and they both return equal rows, so as a newcomer to R, I am not sure what the fix is.

I also ran plot.ts(test.sub) and that works, but it doesn’t show me the dates in the x-axis, which it was doing with plot(test.sub) and which is what I would like to see.

test.sub[1]
              V1
1107 2011-Aug-24
1206 2011-Aug-25
1307 2011-Aug-26
1408 2011-Aug-29
1510 2011-Aug-30
1613 2011-Aug-31
1718 2011-Sep-01
1823 2011-Sep-02
1929 2011-Sep-06
2035 2011-Sep-07
2143 2011-Sep-08
2251 2011-Sep-09
2359 2011-Sep-13
2470 2011-Sep-14
2581 2011-Sep-15
2692 2011-Sep-16
2785 2011-Sep-19
2869 2011-Sep-20
2965 2011-Sep-21
3062 2011-Sep-22
3160 2011-Sep-23
3258 2011-Sep-26
3356 2011-Sep-27
3455 2011-Sep-28
3555 2011-Sep-29
3655 2011-Sep-30
3755 2011-Oct-03
3856 2011-Oct-04
3957 2011-Oct-05
4059 2011-Oct-06
4164 2011-Oct-07
4269 2011-Oct-10
4374 2011-Oct-11
4479 2011-Oct-12
4584 2011-Oct-13
4689 2011-Oct-14

str(test.sub)
'data.frame':   35 obs. of  2 variables:
 $ V1:Class 'Date'  num [1:35] NA NA NA NA NA NA NA NA NA NA ...
 $ V4: num  0.475 0.452 0.423 0.418 0.403 ...

head(test.sub) V1 V4 
1212 <NA> 0.474697 
1313 <NA> 0.451907 
1414 <NA> 0.423184 
1516 <NA> 0.417709 
1620 <NA> 0.402966 
1725 <NA> 0.414264 

Now that this is working, I’d like to add a 3rd variable to plot a 3d chart – any suggestions how I can do that. thx!

  • 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-05-26T05:59:09+00:00Added an answer on May 26, 2026 at 5:59 am

    So I think there are a few things going on here that are worth talking through:

    first, some example data:

    test <- data.frame(End = Sys.Date()+1:5, 
                   Start = Sys.Date()+0:4, 
                   tck = rep("GOOG",5), 
                   EndP= 1:5, 
                   StartP= 0:4)
    
    test.sub = subset(test, tck=="GOOG",select = c(End, EndP))
    

    First, note that test and test.sub are both data frames, so calls like test.sub[1] don’t really “mean” anything to R.** It’s more R-ish to write test.sub[,1] by virtue of consistency with other R structures. If you compare the results of str(test.sub[1]) and str(test.sub[,1]) you’ll see that R treats them slightly differently.

    You said you typed:

    as.ts(test.sub)
    plot(test.sub)
    

    I’d guess you have extensive experience with some sort of OO-language; and while R does have some OO flavor to it, it doesn’t apply here. Rather than transforming test.sub to something of class ts, this just does the transformation and throws it away, then moves on to plot the data frame you started with. It’s an easy fix though:

    test.sub.ts <- as.ts(test.sub)
    plot(test.sub.ts)
    

    But, this probably isn’t what you were looking for either. Rather, R creates a time series that has two variables called “End” (which is the date now coerced to an integer) and “EndP”. Funny business like this is part of the reason time series packages like zoo and xts have caught on so I’ll detail them instead a little further down.

    (Unfortunately, to the best of my understanding, R doesn’t keep date stamps with its default ts class, choosing instead to keep start and end dates as well as a frequency. For more general time series work, this is rarely flexible enough)

    You could perhaps get what you wanted by typing

    plot(test.sub[,1], test.sub[,2]) 
    

    instead of

    plot(test.sub[1], test.sub[2])
    

    since the former runs into trouble given that you are passing two sub-data frames instead of two vectors (even though it looks like you would be).*

    Anyways, with xts (and similarly for zoo):

    library(xts) # You may need to install this
    xtemp <- xts(test.sub[,2], test.sub[,1]) # Create the xts object
    plot(xtemp) 
    # Dispatches a xts plot method which does all sorts of nice time series things
    

    Hope some of this helps and sorry for the inline code that’s not identified as such: still getting used to stack overflow.

    Michael

    **In reality, they access the lists that are used to structure a data frame internally, but that’s more a code nuance than something worth relying on.

    ***The nitty-gritty is that when you pass plot(test.sub[1], test.sub[2]) to R, it dispatches the method plot.data.frame which takes a single data frame and tries to interpret the second data frame as an additional plot parameter which gets misinterpreted somewhere way down the line, giving your error.

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

Sidebar

Related Questions

I am working with a data frame where one of the columns consists of
Having spent some time working on data warehousing, I have created both ETL (extract
I have two (slow loading) data sources. I can start working on data source
The following is the data I am working on: __DATA__ Node 1: 98 debug
I always seem to use Get when working with data (strongly typed or otherwise)
I'm working through the Data Binding chapter in Pro WPF in C# 2008. They
I'm working on a Data Warehouse which, in the end, will require me to
I'm working on a data warehouse project and would like to know how to
I'm working on a data distribution application which receives data from a source and
I'm working with XML data from an application where we get XML like this:

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.