[R] Help using order with data.frame
arun
smartpink111 at yahoo.com
Sun Mar 16 23:02:13 CET 2014
Hi,
Try:
data_test<-data.frame(id, dates, info, sqfootage, lotsize,stringsAsFactors=FALSE)
data_test[order(with(data_test,as.Date(dates,"%m/%d/%Y"))),]
id dates info sqfootage lotsize
3 ID3 2/03/2013 siding 3000 0.75
2 ID2 1/16/2014 wood 2000 0.50
1 ID1 2/16/2014 brick 1000 0.25
4 ID4 4/20/2014 straw 2500 1.00
A.K.
On Sunday, March 16, 2014 5:50 PM, Jason Rupert <jasonkrupert at yahoo.com> wrote:
Thank you very much for your posts. I updated the example, which was taken from the following:
http://cran.r-project.org/doc/contrib/Torfs+Brauer-Short-R-Intro.pdf
Hopefully the below is a bit more representative question, but unfortunately, it looks like I'm not inputting the dates information properly to the order information
In the way I'm currently inputting dates, order is not able to order the dates properly. Again I'm stumbling over subtleties of the R syntax. Again thanks for any guidance, advice, references to addressing ordering dates information.
Thanks again.
id<-c("ID1", "ID2", "ID3", "ID4")
info<-c("brick", "wood", "siding", "straw")
sqfootage<-c(1000, 2000,
3000, 2500)
lotsize<-c(0.25, 0.5, 0.75, 1.0)
data_test<-data.frame(id, dates, info, sqfootage, lotsize)
data_test_sorted<-data_test[order(data_test$dates),]
data_test_sorted
id dates info sqfootage lotsize
2 ID2 1/16/2014 wood 2000 0.50
3 ID3 2/03/2013 siding 3000 0.75
1 ID1 2/16/2014 brick 1000 0.25
4 ID4 4/20/2014 straw 2500 1.00
dates
[1] "2/16/2014" "1/16/2014" "2/03/2013" "4/20/2014"
data_test
id dates info sqfootage lotsize
1 ID1 2/16/2014 brick 1000 0.25
2 ID2 1/16/2014 wood
2000 0.50
3 ID3 2/03/2013 siding 3000 0.75
4 ID4 4/20/2014 straw 2500 1.00
Desire to order the dates information chronologically.
Thanks again for references to help me understand the proper details of the R syntax (which I love!).
On Sunday, March 16, 2014 12:14 AM, Jeff Newmiller <jdnewmil at dcn.davis.CA.us> wrote:
It is not order that you are having difficulty with... it is name scope. Your "z" is not a standalone variable, but a column in your "t" data frame.
Try
t[order(t$z), ]
Note that "t" is the name of a commonly-used function in R that transposes matrices. It is generally not a good idea to reuse names like that, but it can be puzzling to avoid when you are new. I type the name of the variable I want to create alone on an interactive R console before I create it... if it tells me that variable is not defined then I proceed.
You should also avoid posting HTML email to this list, as what we see is often not what you see. Read the Posting Guide for more useful advice on using the list.
---------------------------------------------------------------------------
Jeff Newmiller
The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please
excuse my brevity.
On March 15, 2014 9:21:41 PM PDT, Jason Rupert <jasonkrupert at yahoo.com> wrote:
>Evidently, I'm overlooking something simple.� I'm trying to used order
>with data.frame. �
>
>
>For example:
>
>t = data.frame(x = c(11,12,14), y = c(19,20,21), z = c(10,9,7))
>t[order(z), ]
>Error in order(z) : object 'z' not found
>
>Thank you for any insights and advice provided.
> [[alternative HTML version deleted]]
>
>
>
>------------------------------------------------------------------------
>
>______________________________________________
>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.
More information about the R-help
mailing list