analyst41 at hotmail.com
2011-Jan-12 15:16 UTC
[R] Outputting csv file from dataframe with columns in a particular order
I have a dataframe with columns "ID",'date","estimate","actual" (but not necessarily in that order - I do a merge somewhere and that somehow messes up the order of the columns). How can I output it to a csv file with the columns in the order that I want? Thanks.
Ivan Calandra
2011-Jan-12 15:34 UTC
[R] Outputting csv file from dataframe with columns in a particular order
Hi! Let's say your data.frame is called df and that you want column 1, then column 4, then 3 and then 2: df <- data.frame(ID=LETTERS[1:5], date=rnorm(5), estimate=rnorm(5), actual=rnorm(5)) write.csv(df[c(1,4,3,2)], file="df.csv") HTH, Ivan Le 1/12/2011 16:16, analyst41 at hotmail.com a ?crit :> I have a dataframe with columns "ID",'date","estimate","actual" (but > not necessarily in that order - I do a merge somewhere and that > somehow messes up the order of the columns). > > How can I output it to a csv file with the columns in the order that I > want? > > 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. >-- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. S?ugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra at uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php
Peter Ehlers
2011-Jan-12 15:34 UTC
[R] Outputting csv file from dataframe with columns in a particular order
On 2011-01-12 07:16, analyst41 at hotmail.com wrote:> I have a dataframe with columns "ID",'date","estimate","actual" (but > not necessarily in that order - I do a merge somewhere and that > somehow messes up the order of the columns). > > How can I output it to a csv file with the columns in the order that I > want?Let's say that your data.frame is DF. mynames <- c("ID", "date", "estimate", "actual") write.csv(DF[, mynames], ....) Peter Ehlers
Petr PIKAL
2011-Jan-12 15:36 UTC
[R] Odp: Outputting csv file from dataframe with columns in a particular order
Hi r-help-bounces at r-project.org napsal dne 12.01.2011 16:16:16:> I have a dataframe with columns "ID",'date","estimate","actual" (but > not necessarily in that order - I do a merge somewhere and that > somehow messes up the order of the columns).If you have datafreme with column order a, b, c, d and you want b, c, d, a just order columns accordingly write.table(df[,c("b", "c", "d", "s")], ....) Regards Petr> > How can I output it to a csv file with the columns in the order that I > want? > > Thanks. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.