Hi, I am converting a data.frame to xts. ?the data.frame is 4 columns and 1000 rows. ?I get a message that "NROW (x) must match length(order.by) class is data.frame, mode is list when I run? dim(x) ? # I get? 1000 ? ? 4 ? #which is consistent with 1000 rows and 4 columns NROW (x) ?# I get 1000 ?# which is the right answer ? When I run length on each of columns in x separately using the "$" I get 1000, which is the right number too. So length on each of the columns?individually?gives me the right answer, but length on the data.frame gives me the number of columns instead of the number of rows, is there an explanation thanks
R. Michael Weylandt <michael.weylandt@gmail.com>
2011-Oct-27 19:23 UTC
[R] NROW doesn't equal length(x)
Data frame is list internally so length(df) = ncol(df) M On Oct 27, 2011, at 2:44 PM, Muhammad Abuizzah <izzah100 at yahoo.com> wrote:> Hi, > > I am converting a data.frame to xts. the data.frame is 4 columns and 1000 rows. I get a message that "NROW (x) must match length(order.by) > class is data.frame, mode is list > > when I run > dim(x) # I get > 1000 4 #which is consistent with 1000 rows and 4 columns > > NROW (x) # I get > > 1000 # which is the right answer > > When I run length on each of columns in x separately using the "$" I get 1000, which is the right number too. > So length on each of the columns individually gives me the right answer, but length on the data.frame gives me the number of columns instead of the number of rows, is there an explanation > > > thanks > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Your first problem is that you aren't using paste() properly: print out paste(ct3[, 1:2]) and take a look at it. This works: apply(head(ct3[,1:2]),1,paste,collapse = " ") You also have the format argument to POSIXct wrong. See ?strptime for details. So the whole line (if you want to do it in one) would be something like this: v = xts(ct3[,3], as.POSIXct(apply(ct3[, 1:2],1, paste, collapse = " "), format = "%m/%d/%Y %H:%M:%S")) head(v) 2011-02-22 09:31:13 19.46 2011-02-22 09:31:28 19.50 2011-02-22 09:31:43 19.55 2011-02-22 09:31:58 19.59 2011-02-22 09:32:13 19.67 2011-02-22 09:32:28 19.68 Michael PS -- It's best practice to cc the list as well as me in replies so that this gets archived. On Tue, Nov 1, 2011 at 5:37 PM, Muhammad Abuizzah <izzah100 at yahoo.com> wrote:> I attached a dput of my data file which I am trying to transform to xts. > > The name of my object is ct3, I am putting the generated info into "v" > The code I used to convert it to xts is as follows: > > v = xts(ct3[,3], as.POSIXct(paste (ct3 [,1:2]), format = "%MM/%DD/%YYYY %H:%M:%:S") > > I would appreciate any help in converting this data frame into xts. ?I am not sure is the NROW issue is the reason behind the failure or is it the data formate > > thanks > > > ----- Original Message ----- > From: "R. Michael Weylandt <michael.weylandt at gmail.com>" <michael.weylandt at gmail.com> > To: Muhammad Abuizzah <izzah100 at yahoo.com> > Cc: "r-help at R-project.org" <r-help at r-project.org> > Sent: Thursday, October 27, 2011 3:23 PM > Subject: Re: [R] NROW doesn't equal length(x) > > Data frame is list internally so length(df) = ncol(df) > > M > > On Oct 27, 2011, at 2:44 PM, Muhammad Abuizzah <izzah100 at yahoo.com> wrote: > >> Hi, >> >> I am converting a data.frame to xts.? the data.frame is 4 columns and 1000 rows.? I get a message that "NROW (x) must match length(order.by) >> class is data.frame, mode is list >> >> when I run >> dim(x)? ?# I get >> 1000? ? ?4? ?#which is consistent with 1000 rows and 4 columns >> >> NROW (x)? # I get >> >> 1000? # which is the right answer >> >> When I run length on each of columns in x separately using the "$" I get 1000, which is the right number too. >> So length on each of the columns individually gives me the right answer, but length on the data.frame gives me the number of columns instead of the number of rows, is there an explanation >> >> >> thanks >> >> ______________________________________________ >> R-help at r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >