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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:37:59+00:00 2026-05-28T07:37:59+00:00

I’m trying to create a subset of a data frame and when I do

  • 0

I’m trying to create a subset of a data frame and when I do so, R switches the formatting of the date column. Any idea why or how to fix this?

> head(spyPr2)
        Date   Open   High    Low  Close    Volume Adj.Close
1 12/30/2011 126.02 126.33 125.50 125.50  95599000    125.50
2 12/29/2011 125.24 126.25 124.86 126.12 123507200    126.12
3 12/28/2011 126.51 126.53 124.73 124.83 119107100    124.83
4 12/27/2011 126.17 126.82 126.06 126.49  86075700    126.49
5 12/23/2011 125.67 126.43 125.41 126.39  92187200    126.39
6 12/22/2011 124.63 125.40 124.23 125.27 119465400    125.27
> spyPr2$Date <- as.Date(spyPr2$Date, format = "%m/%d/%Y")
> head(spyPr2)
        Date   Open   High    Low  Close    Volume Adj.Close
1 2011-12-30 126.02 126.33 125.50 125.50  95599000    125.50
2 2011-12-29 125.24 126.25 124.86 126.12 123507200    126.12
3 2011-12-28 126.51 126.53 124.73 124.83 119107100    124.83
4 2011-12-27 126.17 126.82 126.06 126.49  86075700    126.49
5 2011-12-23 125.67 126.43 125.41 126.39  92187200    126.39
6 2011-12-22 124.63 125.40 124.23 125.27 119465400    125.27
> spyPr2 <- data.frame(cbind(spyPr2$Date, spyPr2$Close, spyPr2$Adj.Close))
> str(spyPr2)
'data.frame':   1638 obs. of  3 variables:
 $ X1: num  15338 15337 15336 15335 15331 ...
 $ X2: num  126 126 125 126 126 ...
 $ X3: num  126 126 125 126 126 ...
> head(spyPr2)
     X1     X2     X3
1 15338 125.50 125.50
2 15337 126.12 126.12
3 15336 124.83 124.83
4 15335 126.49 126.49
5 15331 126.39 126.39
6 15330 125.27 125.27

UPDATE:

> spyPr2 <- data.frame(cbind(spyPr2["Date"], spyPr2$Close, spyPr2$Adj.Close))
Error in `[.data.frame`(spyPr2, "Date") : undefined columns selected
> spyPr2 <- data.frame(cbind(spyPr2[,"Date"], spyPr2$Close, spyPr2$Adj.Close))
Error in `[.data.frame`(spyPr2, , "Date") : undefined columns selected

UPDATE 2:

structure(list(Date = structure(c(15338, 15337, 15336, 15335, 
15331, 15330), class = "Date"), Open = c(126.02, 125.24, 126.51, 
126.17, 125.67, 124.63), High = c(126.33, 126.25, 126.53, 126.82, 
126.43, 125.4), Low = c(125.5, 124.86, 124.73, 126.06, 125.41, 
124.23), Close = c(125.5, 126.12, 124.83, 126.49, 126.39, 125.27
), Volume = c(95599000L, 123507200L, 119107100L, 86075700L, 92187200L, 
119465400L), Adj.Close = c(125.5, 126.12, 124.83, 126.49, 126.39, 
125.27)), .Names = c("Date", "Open", "High", "Low", "Close", 
"Volume", "Adj.Close"), row.names = c(NA, -6L), class = "data.frame")
  • 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-28T07:37:59+00:00Added an answer on May 28, 2026 at 7:37 am

    Obvious answer is don’t do subsetting like that! Use the appropriate tools. What is wrong with

    spyPr2.new <- spyPr2[, c("Date", "Close", "Adj.Close")]
    

    ?

    To explain the behaviour you are seeing, you need to understand what $ returns and how cbind() works. cbind() is one of those oddities in R wherein method dispatch is not done via the usual method but is instead handled via special code buried in the internals of R. This is all the R code behind cbind():

    > cbind
    function (..., deparse.level = 1) 
    .Internal(cbind(deparse.level, ...))
    <bytecode: 0x24fa0c0>
    <environment: namespace:base>
    

    Not much help, eh? There are methods for data frames and "ts" objects however:

    > methods(cbind)
    [1] cbind.data.frame cbind.ts*       
    
       Non-visible functions are asterisked
    

    Before I do the reveal, also note what $ returns (dat2 is your 6 lines of data after converting Date to a "Date" object):

    > str(dat2$Date)
     Date[1:6], format: "2011-12-30" "2011-12-29" "2011-12-28" "2011-12-27" ...
    

    This is a "Date" object, which is a special vector really.

    > class(dat2$Date)
    [1] "Date"
    

    The key thing is that it is not a data frame. So when you use cbind(), the internal code is seeing three vectors and the internal code creates a matrix.

    > (c1 <- cbind(dat2$Date, dat2$Close, dat2$Adj.Close))
          [,1]   [,2]   [,3]
    [1,] 15338 125.50 125.50
    [2,] 15337 126.12 126.12
    [3,] 15336 124.83 124.83
    [4,] 15335 126.49 126.49
    [5,] 15331 126.39 126.39
    [6,] 15330 125.27 125.27
    > class(c1)
    [1] "matrix"
    

    There can only be numeric or character matrices in R so the Date object is converted to a numeric vector:

    > as.numeric(dat2$Date)
    [1] 15338 15337 15336 15335 15331 15330
    

    to allow cbind() to produce a numeric matrix.

    You can force the use of the data frame method by calling it explicitly and it does know how to handle "Date" objects and so doesn’t do any conversion:

    > cbind.data.frame(dat2$Date, dat2$Close, dat2$Adj.Close)
       dat2$Date dat2$Close dat2$Adj.Close
    1 2011-12-30     125.50         125.50
    2 2011-12-29     126.12         126.12
    3 2011-12-28     124.83         124.83
    4 2011-12-27     126.49         126.49
    5 2011-12-23     126.39         126.39
    6 2011-12-22     125.27         125.27
    

    However, all the explanation aside, you are trying to do the subsetting in a very complex manner. [ as a subset function works just fine:

    > dat2[, c("Date", "Close", "Adj.Close")]
            Date  Close Adj.Close
    1 2011-12-30 125.50    125.50
    2 2011-12-29 126.12    126.12
    3 2011-12-28 124.83    124.83
    4 2011-12-27 126.49    126.49
    5 2011-12-23 126.39    126.39
    6 2011-12-22 125.27    125.27
    

    subset() is also an option but not needed here:

    > subset(dat2, select = c("Date", "Close", "Adj.Close"))
            Date  Close Adj.Close
    1 2011-12-30 125.50    125.50
    2 2011-12-29 126.12    126.12
    3 2011-12-28 124.83    124.83
    4 2011-12-27 126.49    126.49
    5 2011-12-23 126.39    126.39
    6 2011-12-22 125.27    125.27
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to construct a data frame in an Rcpp function, but when I
I'm trying to create an if statement in PHP that prevents a single post
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

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.