[R] Sorting data frame by a string variable

Peter Dalgaard p.dalgaard at biostat.ku.dk
Tue Jul 17 20:13:16 CEST 2007


Dimitri Liakhovitski wrote:
> I have a data frame MyData with 2 variables.
> One of the variables (String) contains a string of letters.
> How can I resort MyData by MyData$String (alphabetically) and then
> save the output as a sorted data file?
>
> I tried:
>
> o<-order(MyData$String)
> SortedData<-rbind(MyData$String[o], MyData$Value[o])
> write.table(SortedData,file="Sorted.txt",sep="\t",quote=F, row.names=F)
>
>
> However, all strings get replaced with digits (1 for the first string,
> 2 for the second string etc.). How can I keep the strings instead of
> digits?
>   

Why on earth are you trying to rbind() things together?

Anything wrong with

SortedData <- MyData[o,]
write.table(SortedData,...whatever...)

?

> Thank you!
> Dimitri
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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